Loguru 停止在生产中记录烧瓶异常

chr*_*ton 5 python flask

我正在使用 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)

Run Code Online (Sandbox Code Playgroud)

chr*_*ton 3

弄清楚了问题。默认情况下,使用 werkzeug 开发服务器,它使用 messages/events.log 文件。当我将应用程序部署到 apache 服务器时,它重新路由了本来放置在这里的日志,并将它们与 apache 服务器日志放在一起