简体   繁体   English

如何在GAE Python logging.info日志中获取文件名和行号?

[英]How to have file name and line number in GAE Python logging.info logs?

In the logs, the timestamp is available but the "source file name" and "line number" are not logged with every log message. 在日志中,时间戳可用,但每个日志消息都不记录"source file name""line number"

How to log file name and line number in GAE Python logging.info (or logging.warn or logging.error and others) ? 如何在GAE Python logging.info (或logging.warnlogging.error等)中记录文件名和行号?

By "source file name" , I mean the Python code file (*.py) which generated the log. "source file name"是指生成日志的Python代码文件(* .py)。

In your main.py add: 在你的main.py添加:

formatter = logging.Formatter('%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s')

logging.getLogger().handlers[0].setFormatter(formatter)

Have you tried looking at logging formatters ? 您是否尝试过查看记录格式化程序

It should look something like this 看起来应该是这样的

formatter = logging.Formatter(
    '[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d}'
    ' %(levelname)s - %(message)s','%m-%d %H:%M:%S')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM