gpt4 book ai didi

python - 在 Django 日志记录中使用 str.format()

转载 作者:行者123 更新时间:2023-11-28 17:44:47 25 4
gpt4 key购买 nike

我在 Ubuntu 12.04 中使用 django 1.6 和 python 3.2。我发现 django 日志记录正在使用 python 的内置日志记录模块。作为 dictConfig format(python 3.x version)说,当 django 使用 dictConfig format(python 2.x version) 时,我可以在设置日志格式化程序时使用 3 种格式样式(%-格式化、{}-格式化和 $()-格式化)作为文档。

那么我可以在 Django 日志记录中使用不同类型的格式样式吗?

喜欢:

{
'format':"{levelname} - {asctime} - {module} - {process} - {thread} - {message}"
}

代替

{
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
}

在 Django 1.6 中?

最佳答案

您应该能够做到这一点,因为您可以控制您使用的格式化程序。只需在定义格式化程序参数的字典中指定合适的 style 值,例如

{
"format": "{levelname} - {asctime} - {module} - {message}",
"style": "{",
}

更新:对于 Python 3.2,您需要使用此字典指定格式化程序:

{
'()': logging.Formatter,
'format': '{levelname} - {asctime} - {module} - {message}',
'style': '{',
},

在 Python 3.3 或更高版本上,不需要 '()': logging.Formatter 行。

关于python - 在 Django 日志记录中使用 str.format(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018789/

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