【问题标题】:use str.format() in django logging在 django 日志记录中使用 str.format()
【发布时间】:2013-11-29 21:23:52
【问题描述】:

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

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

喜欢:

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

而不是

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

在 django 1.6 中?

【问题讨论】:

  • 我根据您的评论更新了我的答案。

标签: python django string logging format


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 我更改了格式化程序设置,但它不起作用。仅在日志文件中记录{levelname} - {asctime} - {module} - {message}。格式化程序设置仅使用 %-style 。
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 2011-09-16
  • 1970-01-01
  • 2011-04-06
  • 2010-09-25
  • 2011-10-21
  • 2018-06-05
  • 2013-09-11
相关资源
最近更新 更多