配置日志系统
django框架logging配置
配置setting
LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters':{ "generic": { "format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s", "datefmt": "[%Y-%m-%d %H:%M:%S %z]", "class": "logging.Formatter" }, "access": { "format": "%(message)s", "class": "logging.Formatter" } }, 'loggers': { "AuthCert.error": { "level": "INFO", "handlers": ["error_file"], "propagate": 1, "qualname": "AuthCert.error" }, "AuthCert.access": { "level": "INFO", "handlers": ["access_file"], "propagate": 0, "qualname": "AuthCert.access" } }, 'handlers': { "console": { "class": "logging.StreamHandler", "formatter": "generic", "stream": "sys.stdout" }, "error_file": { "class": "logging.FileHandler", "formatter": "generic", "filename": "./log/error.log" }, "access_file": { "class": "logging.handlers.RotatingFileHandler", "maxBytes": 1024 * 1024, "backupCount": 5, "formatter": "generic", "filename": "./log/acess.log", } }, }
配置函数
import logging logger = logging.getLogger('AuthCert.error')
写日志
logger.info('增加用户成功') logger.info('success')
效果
error可以看见所有的日志信息

Flask框架logging项目配置--方式一
配置loggin
#逻辑代码中配置
1 import logging
2
3 # 1、创建一个logger
4 logger = logging.getLogger('mylogger')
5 logger.setLevel(logging.DEBUG)
6
7 # 2、创建一个handler,用于写入日志文件
8 fh = logging.FileHandler('log.log')
9 fh.setLevel(logging.DEBUG)
10
11 # 再创建一个handler,用于输出到控制台
12 ch = logging.StreamHandler()
13 ch.setLevel(logging.DEBUG)
14
15 # 3、定义handler的输出格式(formatter)
16 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
17
18 # 4、给handler添加formatter
19 fh.setFormatter(formatter)
20 ch.setFormatter(formatter)
21
22 # 5、给logger添加handler
23 logger.addHandler(fh)
24 logger.addHandler(ch)
# 获取当前登录用户总数 def get_user_count(): sql = "SELECT ID, user_login FROM wp_users GROUP BY ID" id_list = [] try: cursor = db.cursor() cursor.execute(sql) results = cursor.fetchall()# 获取用户信息for row in results: id = row[0] id_list.append(id) idlist = set(id_list) id_list = list(idlist) logger.info('success get_user_count') return len(id_list) except Exception as e: logger.error(str(e)) return id_list
访问项目
在文件目录生产了log.log文件

Flask框架logging项目配置--方式二
#__init__.py
import logging # 配置日志信息 # 创建日志记录器,指明日志保存的路径、每个日志文件的最大大小、保存的日志文件个数上限 file_log_handler = RotatingFileHandler("logs/log", maxBytes=1024*1024*100, backupCount=10) # 创建日志记录的格式 日志等级 输入日志信息的文件名 行数 日志信息 formatter = logging.Formatter('%(levelname)s %(filename)s:%(lineno)d %(message)s') # 为刚创建的日志记录器设置日志记录格式 file_log_handler.setFormatter(formatter) # 为全局的日志工具对象(flask app使用的)添加日记录器 logging.getLogger().addHandler(file_log_handler) # 设置日志的记录等级 logging.basicConfig(level=logging.DEBUG) # 调试debug级
# demo.py
# import logging
from flask import current_app
@api.route("/index")
def index():
#print("hello")
# logging.error() # 记录错误信息
# logging.warn() # 警告
# logging.info() # 信息
# logging.debug() # 调试
current_app.logger.error("error info")
current_app.logger.warn("warn info")
current_app.logger.info("info info")
current_app.logger.debug("debug info")
return "index page"
浙公网安备 33010602011771号