gpt4 book ai didi

python - 如何在 Python 中记录源文件名和行号

转载 作者:IT老高 更新时间:2023-10-28 21:08:54 24 4
gpt4 key购买 nike

是否可以装饰/扩展 python 标准日志记录系统,以便在调用日志记录方法时,它还会记录文件和调用它的行号或调用它的方法?

最佳答案

当然,检查 formatters在记录文档中。特别是 lineno 和 pathname 变量。

%(pathname)s Full pathname of the source file where the logging call was issued(if available).

%(filename)s Filename portion of pathname.

%(module)s Module (name portion of filename).

%(funcName)s Name of function containing the logging call.

%(lineno)d Source line number where the logging call was issued (if available).

看起来像这样:

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

关于python - 如何在 Python 中记录源文件名和行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/533048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com