目標(biāo)
通過
gunicorn
啟動app
后可以將info、error
等事件寫入日志文件差导,帶有錯誤所在模塊-函數(shù)-行數(shù)
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·默認(rèn)的gunicorn.glogging.Logger
為自定義日志類
gunicorn --logger-class "run.CustomLogger" -c deploy_config.py run:app