Flask+gunicorn 配置日志
2020-05-17 本文已影响0人
no_ones
目标
通过
gunicorn
启动app
后可以将info、error
等事件写入日志文件,带有错误所在模块-函数-行数
run.py
from gunicorn import glogging
......
class CustomLogger(glogging.Logger):
"""Custom logger for Gunicorn log messages."""
def setup(self, cfg):
"""Configure Gunicorn application logging configuration."""
super().setup(cfg)
# Override Gunicorn's `error_log` configuration.
self._set_handler(
self.error_log, cfg.errorlog, logging.Formatter(
fmt=('timestamp=%(asctime)s pid=%(process)d '
'loglevel=%(levelname)s code=%(filename)s-%(funcName)s-%(lineno)s '
'msg=%(message)s')))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True,threaded=True)
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel('INFO') #<<-特别注意,否则info级别不能打印
启动指令
更换·logger_class·默认的gunicorn.glogging.Logger
为自定义日志类
gunicorn --logger-class "run.CustomLogger" -c deploy_config.py run:app