python 记录 : is it possible to add module name to formatter

标签 python

可以通过模块名称获取记录器。像这样:

logging.getLogger(module_name)

我想将 module_name 添加到每条日志记录中。是否可以设置一个添加 module_nameFormatter 对象?

最佳答案

您正在寻找%(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/

相关文章:

python - string.Formatter 不接受 "{}"模板?

python - 使用 django 设置 sqlite3 时遇到问题! :/

python - Dask 中具有可重用自定义图表的机会缓存

python - XPath/Python - 如何在 <div> 中获取不同的 html 标签和文本

python2.7 失败并出现 ImportError : No module named time under cygwin

python - 创建多维零点 Python

python - 如何在二维数组中使用 numpy.searchsorted 进行矢量化

python - 使用当前ec2公共(public)IP配置apache虚拟主机

python - csv行导入到python数组中

python - Tensorflow C++ 推理结果与 Keras 推理略有不同