我刚接触Python,但对Java很有经验。其中一个更有用的技巧是让slf4j后端包含日志语句的源行的文件名和行号,这样单击时就可以导航到源行。
我希望在Python中有同样的工具,但对生态系统缺乏经验。
我如何配置Python日志库来添加它?
发布于 2020-08-11 14:09:03
您可以在记录器的格式化程序中使用任何LogRecord attributes。
import logging
logging.basicConfig(format="File: %(filename)s Line: %(lineno)d Message: %(message)s")
logging.warning("this is a log")输出:File: lineno.py Line: 4 Message: this is a log
发布于 2020-08-11 17:58:48
我通过实验发现,以下用于File "X", line Y的代码片段在PyCharm中生成了可点击的日志。(该片段看起来像一个堆栈跟踪行)。
logging.basicConfig(format='%(levelname)s:%(message)s File "%(pathname)s", line %(lineno)s', level=logging.INFO)示例输出:
INFO:Found latest date date: None - File "(omitted)\DbConnector.py", line 27
INFO:Did not find latest date date, setting to: 2020-08-11 11:52:43.547400 - File "(omitted)\DbConnector.py", line 31对于Python中的记录堆栈跟踪,如下所示:
Traceback (most recent call last):
File "...\pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "...\X.py", line 94, in extract_zip_file
with sftp.open(zip_path, "r") as fi:
TypeError: open() takes 2 positional arguments but 3 were givenhttps://stackoverflow.com/questions/63341400
复制相似问题