在 Flask 中配置 logger 用到的 Python 中原生的 logging 模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: wxnacy([email protected])
import logging import logging.handlers from logging import Formatter
from flask import Flask
app = Flask(__name__)
# 日志格式化配置 fmt = '[%(asctime)s] [%(filename)s:%(lineno)d\t] [%(levelname)s] '\ '[%(url)s] %(message)s ' fmt = RequestFormatter(fmt) stream_handler = logging.StreamHandler() stream_handler.setFormatter(fmt) app.logger.addHandler(stream_handler) app.logger.setLevel(logging.DEBUG)
# rotating file 配置 file_handler = logging.handlers.RotatingFileHandler( 'log/tmd.log', maxBytes=104857600, backupCount=20 ) file_handler.setFormatter(fmt) app.logger.addHandler(file_handler)
app.logger.debug('Hello World')
|
更多的格式化参数