【发布时间】:2016-12-18 06:05:43
【问题描述】:
我遵循了 django 文档的建议,并像这样使用日志记录:
import logging
logger = logging.getLogger(__name__)
def today(...):
logger.info('Sun is shining, the weather is sweet')
使用我当前的配置,输出如下所示:
2016-08-11 14:54:06 mylib.foo.today: INFO Sun is shining, the weather is sweet
不幸的是,一些我无法修改的库使用这样的日志记录:
import logging
def third_party(...):
logging.info('Make you want to move your dancing feet')
不幸的是,输出如下所示:
2016-08-09 08:28:04 root.third_party: INFO Make you want to move your dancing feet
我想看看这个:
2016-08-09 08:28:04 other_lib.some_file.third_party: INFO Make you want to move your dancing feet
区别:
root.third_party ==> other_lib.some_file.third_party
如果代码使用logging.info() 而不是logger.info(),我想查看长版本(不是root)
更新
这不是Elegant setup of Python logging in Django的重复,因为它的解决方案是:
报价开始
在每个模块中,我定义了一个记录器使用
logger = logging.getLogger(__name__)
引用结束。
不,我不会修改使用logging.info() 而不是logger.info() 的第三方代码。
跟进问题
Avoid logger=logging.getLogger(__name__) without loosing way to filter logs
【问题讨论】:
-
docs.djangoproject.com/en/1.9/topics/logging 你应该在你的settings.py中配置它
-
@be_good_do_good 是的,我配置了日志记录。问题是:如何处理未以
logger = logging.getLogger(__name__)命名的记录器 -
另外,我可能会向上游库提交错误报告。没有任何借口不在模块中使用
logger = logging.getLogger(__name__)。 -
你能通过记录文件名部分解决这个问题吗?文件名应该尊重 PYTHONPATH,因此应该是唯一的。它不是记录器名称,但应该非常接近。