我正在使用 Loguru 来处理我的 Flask rest api 中的日志记录。在本地测试应用程序时,它完全按预期记录。当我将我的应用程序部署到运行 apache 的 linux 服务器时,日志记录停止。我可以使用 python app.py
在服务器上手动运行该应用程序并且日志再次工作,但这只会启动开发服务器。
from flask import Flask
from loguru import logger
import logging
import os
class InterceptHandler(logging.Handler):
def emit(self, record):
# Retrieve context where the logging call occurred, this happens to be in the 6th frame upward
logger_opt = logger.opt(depth=6, exception=record.exc_info)
logger_opt.log(record.levelno, record.getMessage())
# create the Flask application
app = Flask(__name__)
logger.add(
'logs/events.log',
level='DEBUG',
format='{time} {level} {message}',
backtrace=True,
rotation='5 MB',
retention=9
)
app.logger.addHandler(InterceptHandler())
logging.basicConfig(handlers=[InterceptHandler()], level=20)
if __name__ == '__main__':
app.run(debug=False)
最佳答案
想通了这个问题。默认情况下,使用 werkzeug 开发服务器时,它使用的是 logs/events.log 文件。当我将应用程序部署到 apache 服务器时,它重新路由了本来放置在这里的日志,并将它们与 apache 服务器日志一起放入
关于python - Loguru 停止在生产中记录 flask 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59973735/