可以通过模块名称获取记录器。像这样:
logging.getLogger(module_name)
我想将 module_name
添加到每条日志记录中。是否可以设置一个添加 module_name
的 Formatter
对象?
最佳答案
您正在寻找%(name)s
参数;将其添加到您的格式化程序模式中:
FORMAT = "%(name)s: %(message)s"
logging.basicConfig(format=FORMAT)
或者在创建 Formatter()
时:
FORMAT = "%(name)s: %(message)s"
formatter = logging.Formatter(fmt=FORMAT)
参见 LogRecord
attributes reference :
Attribute name:
name
Format:%(name)s
Description: Name of the logger used to log the call.
关于 python 记录 : is it possible to add module name to formatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392870/