flask日志收集
配置文件
# logging.json
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"brief": {
"class": "logging.Formatter",
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": "%(asctime)s, %(levelname)s - %(name)s: %(message)s"
},
"single-line": {
"class": "logging.Formatter",
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": "%(asctime)s, %(levelname)s - %(name)s %(pathname)s:%(funcName)s - %(lineno)s: %(message)s"
},
"verbose": {
"class": "logging.Formatter",
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": "%(asctime)s, %(levelname)s - [%(process)s] - %(threadName)s - %(name)s %(pathname)s:%(funcName)s - %(lineno)s: %(message)s"
},
"json": {
"class": "logging.Formatter",
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": "{\n\t\"time\": %(asctime)s\n\t\"level\": %(levelname)s\n\t\"logger\": %(name)s\n\t\"message\": %(message)s\n}"
}
},
"handlers": {
"console.info.handler": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "brief",
"stream": "ext://sys.stdout"
},
"console.debug.handler": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "verbose",
"stream": "ext://sys.stdout"
},
"email.handler": {
"level": "CRITICAL",
"class": "logging.handlers.SMTPHandler",
"formatter": "single-line",
"mailhost": [
"smtp.exmail.qq.com",
25
],
"fromaddr": "report@camera360.com",
"toaddrs": [
"chenlin@camera360.com"
],
"credentials": [
"report@camera360.com",
"9Pf5A&k$f2wX1"
],
"subject": "Program run critical, Pls. check it."
},
"dragon.file.handler.verbose": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "verbose",
"filename": "/tmp/dragon.file.handler.log",
"maxBytes": 20971520,
"backupCount": 20,
"encoding": "utf8"
},
"dragon.file.handler.brief": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "brief",
"filename": "/tmp/dragon.file.handler.log",
"maxBytes": 20971520,
"backupCount": 20,
"encoding": "utf8"
},
"dragon.file.handler.timed": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "DEBUG",
"formatter": "verbose",
"filename": "/tmp/my_log",
"when": "D",
"backupCount": 20,
"encoding": "utf8"
}
},
"root": {
"level": "INFO",
"handlers": [
"console.info.handler"
]
},
"loggers": {
"dragon.app": {
"handlers": [
"dragon.file.handler.brief"
],
"level": "INFO",
"propagate": true
},
"dragon.app.verbose": {
"handlers": [
"dragon.file.handler.verbose"
],
"level": "DEBUG",
"propagate": true
},
"dragon.app.timed": {
"handlers": [
"dragon.file.handler.timed"
],
"level": "DEBUG",
"propagate": true
}
}
}
flask日志收集
import os
import json
import logging
import logging.config
from flask import Flask, request
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def setup_logging(default_level=logging.DEBUG, env_key='LOG_CFG'):
path = os.path.join(BASE_DIR, 'logging.json')
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
app = Flask(__name__)
app_logger = logging.getLogger('dragon.app.timed')
@app.route('/')
def hello():
app_logger.info("%s welcome", request.url)
return "hello world"
if __name__ == '__main__':
setup_logging()
app.run(debug=True)
滚动时间算法
- 起始时间 = 日志文件的最后修改时间
- 下次滚动时间 = 起始时间 + 1天(因为我设置的when=’D’)
- 创建Handler时,把when=’D’改成when=’MIDNIGHT’,表示只要过了0点就会滚动,不会读取日志最后修改时间