flask之日志的配置
1. 项目中,日志和配置文件都是单独在一个文件夹中,一般log文件夹和config文件夹,两个文件夹和manage.py在同一个目录下。
2. 配置日志前,先给flask装上script脚本扩展,Flask 1.1.1 版本
from flask.ext.script import Manager # 报错 no module named flask.ext 解决办法:from flask_script import Manager 网上说是新版flask推荐这种方式导入扩展包 # 继续报错:no module named flask_script 解决办法:pip install flask_script # 继续报错: ModuleNotFoundError: No module named 'setuptools' ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. # 分析,原来是 没有安装 setuptools # 安装 setuptools pip install setuptools # pip install flask_script # 然后解决问题 from flask_script import Manager
用Manager来管理app
manage.py
from logs.logs import create_app, setup_log from flask import url_for,redirect from flask_script import Manager from config.config import Config import logging app = create_app("development") # 这里通过一个函数创建了app的实例对象 manage = Manager(app) # 创建Manager的实例对象,参数是app对象 @app.route("/") def index(): logging.debug("测试打印日志") # logging.warning("测试打印日志") # logging.error("测试打印日志") return "index page" # 传递路径参数,通过视图函数的形参传递 @app.route("/hello/<username>") def hello(username): logging.debug(username) return "hello page" + username # 转换器:可以将传递的参数进行类型转换 @app.route("/user/<int:p_id>") def converter_int(p_id): logging.warning(str(p_id)) return "p_id: " + str(p_id) # 构建url @app.route("/new") def make_url(): """ url_for(视图函数的名字,链接后面的参数) :return: """ new_url = url_for('hello', username="zhangyanyan") print(new_url) return redirect(new_url) if __name__ == '__main__':
manage.run() 启动方式变成:manage.py目录下,python manage.py runserver 启动项目
logs/logs.py
import logging from logging.handlers import RotatingFileHandler from flask import Flask from config.config import config def setup_log(config_name): """ :param config_name: 传入日志等级 :return: """ # 设置日志的的登记 logging.basicConfig(level=config[config_name].LOG_LEVEL) # 创建日志记录器,设置日志的保存路径和每个日志的大小和日志的总大小 file_log_handler = RotatingFileHandler("logs/log", maxBytes=1024*1024*100,backupCount=100) # 创建日志记录格式,日志等级,输出日志的文件名 行数 日志信息 formatter = logging.Formatter("%(levelname)s %(filename)s: %(lineno)d %(message)s") # 为日志记录器设置记录格式 file_log_handler.setFormatter(formatter) # 为全局的日志工具对象(flaks app使用的)加载日志记录器 logging.getLogger().addHandler(file_log_handler) def create_app(config_name): """ :param config_name: info bug error :return: """ # 创建app实例前先配置好日志文件 setup_log(config_name) # 创建app实例对象 app = Flask(__name__) # 实例对象从配置文件中加载配置 app.config.from_object(config[config_name]) # 这里直接拿到的是类的名字,也就是引用 return app
config/config.py
import logging class Config(object): # 设置日志等级 LOG_LEVEL = logging.DEBUG class DevelopConfig(Config): """开发环境下的配置""" DEBUG = True class ProductConfig(Config): """生成环境下的配置""" DEBUG = False LOG_LEVEL = logging.WARNING class TestConfig(Config): """测试环境下的配置""" DEBUG = True TESTING = True config = { "development": DevelopConfig, "production": ProductConfig, "testing": TestConfig, }
logs/log文件
INFO _internal.py: 122 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) INFO _internal.py: 122 * Restarting with stat WARNING _internal.py: 122 * Debugger is active! INFO _internal.py: 122 * Debugger PIN: 340-059-688 DEBUG manage.py: 13 测试打印日志 WARNING manage.py: 14 测试打印日志 ERROR manage.py: 15 测试打印日志 INFO _internal.py: 122 127.0.0.1 - - [24/Nov/2019 15:10:29] "GET / HTTP/1.1" 200 - INFO _internal.py: 122 * Detected change in '/Users/xxx/PycharmProjects/flask_project1/manage.py', reloading INFO _internal.py: 122 * Restarting with stat WARNING _internal.py: 122 * Debugger is active! INFO _internal.py: 122 * Debugger PIN: 340-059-688 DEBUG manage.py: 22 zhang INFO _internal.py: 122 127.0.0.1 - - [24/Nov/2019 15:12:01] "GET /hello/zhang HTTP/1.1" 200 - INFO _internal.py: 122 * Detected change in '/Users/xxx/PycharmProjects/flask_project1/manage.py', reloading INFO _internal.py: 122 * Restarting with stat WARNING _internal.py: 122 * Debugger is active! INFO _internal.py: 122 * Debugger PIN: 340-059-688
config.py中还可以进行好多的配置
例如:数据库的配置,密码的配置
具体的配置参看:https://www.cnblogs.com/alicelai1319/p/10289798.html