[英]StreamHandler - Not logging to sys.stdout in docker container - Python
I have a RotatingFileHandler and StreamHandler in my Python app.我的 Python 应用程序中有 RotatingFileHandler 和 StreamHandler。 This is running in an apache server in a docker container.
这是在 docker 容器中的 apache 服务器中运行的。 So I have symlinked both the apache access and error logs to the /dev/stdout seen as:
所以我将 apache 访问和错误日志符号链接到 /dev/stdout,如下所示:
RUN ln -sf /dev/stdout /project/Project_Service/log/access.log && \
ln -sf /dev/stdout /project/Project_Service/log/error.log
I have set up the handlers as:我已将处理程序设置为:
app = Flask(__name__)
app.logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler = RotatingFileHandler(app_error,
maxBytes=1024 * 1024 * 100, backupCount=20)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler(stream=sys.stdout)
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(formatter)
app.logger.addHandler(file_handler)
app.logger.addHandler(stream_handler)
app.logger.debug('DEBUG LOG')
app.logger.info('INFO LOG')
app.logger.warning('WARNING LOG')
app.logger.error('ERROR LOG')
app.logger.critical('CRITICAL LOG')
If i look at the app_error file, i can see output correctly:如果我查看 app_error 文件,我可以正确地看到输出:
2018-12-06 19:37:46,863 - app - DEBUG - DEBUG LOG
2018-12-06 19:37:46,865 - app - INFO - INFO LOG
2018-12-06 19:37:46,865 - app - WARNING - WARNING LOG
2018-12-06 19:37:46,866 - app - ERROR - ERROR LOG
2018-12-06 19:37:46,868 - app - CRITICAL - CRITICAL LOG
And this is seen in the docker logs:这在 docker 日志中可以看到:
[Thu Dec 06 19:37:46.864764 2018] [:error] [pid 8] 2018-12-06 19:37:46,863 - app - DEBUG - DEBUG LOG
[Thu Dec 06 19:37:46.865872 2018] [:error] [pid 8] 2018-12-06 19:37:46,865 - app - INFO - INFO LOG
[Thu Dec 06 19:37:46.866116 2018] [:error] [pid 8] 2018-12-06 19:37:46,865 - app - WARNING - WARNING LOG
[Thu Dec 06 19:37:46.866370 2018] [:error] [pid 8] [2018-12-06 19:37:46,866] ERROR in app: ERROR LOG
[Thu Dec 06 19:37:46.868450 2018] [:error] [pid 8] 2018-12-06 19:37:46,866 - app - ERROR - ERROR LOG
[Thu Dec 06 19:37:46.870184 2018] [:error] [pid 8] [2018-12-06 19:37:46,868] CRITICAL in app: CRITICAL LOG
[Thu Dec 06 19:37:46.870448 2018] [:error] [pid 8] 2018-12-06 19:37:46,868 - app - CRITICAL - CRITICAL LOG
Which makes sense, as i have both the error log, which i'm assuming has log levels of ERROR and CRITICAL, and the streamhandler.这是有道理的,因为我同时拥有错误日志(我假设它的日志级别为 ERROR 和 CRITICAL)和流处理程序。 However, if i take out the symlink to the error log, both logs disappear.
但是,如果我取出错误日志的符号链接,两个日志都会消失。 It's as if the StreamHandler is outputting to dev/stderr, even though i have declared it as sys.stdout in the StreamHandler.
就好像 StreamHandler 正在输出到 dev/stderr,即使我已经在 StreamHandler 中将其声明为 sys.stdout。 I've also tried removing the "stream=", but that did not fix anything.
我也试过删除“stream=”,但没有解决任何问题。 I've been pouring over all the documentation, and can't seem to figure out how to fix this.
我一直在翻阅所有文档,似乎无法弄清楚如何解决这个问题。
I've also set PYTHONUNBUFFERED=0
in the environment variables.我还在环境变量中设置
PYTHONUNBUFFERED=0
。
Any help or direction you can provide is appreciated.感谢您提供的任何帮助或指导。
Flask attaches a "default" StreamHandler to the app logger already. Flask 已经将“默认” StreamHandler 附加到应用程序记录器。 Thus after you attach your 2 loggers there's actually 3 loggers:
因此,在您连接 2 个记录器后,实际上有 3 个记录器:
print(app.logger.handlers)
[<logging.StreamHandler object at 0x7ff30592b358>,
<logging.handlers.RotatingFileHandler object at 0x7ff3058e2e80>,
<logging.StreamHandler object at 0x7ff3058f33c8>]
This also explains why the 2 lines have a different format -- they come from the default StreamHandler which has its own formatter:这也解释了为什么这两行有不同的格式——它们来自默认的 StreamHandler,它有自己的格式化程序:
2018-12-06 19:37:46,863 - app - DEBUG - DEBUG LOG
2018-12-06 19:37:46,865 - app - INFO - INFO LOG
2018-12-06 19:37:46,865 - app - WARNING - WARNING LOG
[2018-12-06 19:37:46,866] ERROR in app: ERROR LOG # <----
2018-12-06 19:37:46,866 - app - ERROR - ERROR LOG
[2018-12-06 19:37:46,868] CRITICAL in app: CRITICAL LOG # <----
2018-12-06 19:37:46,868 - app - CRITICAL - CRITICAL LOG
The fix is to simply remove all handlers before you add yours :解决方法是在添加您的处理程序之前简单地删除所有处理程序:
app.logger.handlers = []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.