如何用python中的日志API记录keyvalue对

2024-04-29 08:07:16 发布

您现在位置:Python中文网/ 问答频道 /正文

第一个条件是我正在使用日志API/模块,现在不能更改到任何其他库。在

我想让我的日志文件写像下面的键值对。在

2017-03-26 00:34:02,760 - root - WARNING - There is something burning. Where=Kitchen House=Alice

所以在这里Where=Kitchen和{}是键值对。在

我知道我能做到

^{pr2}$

但它会印出来的

2017-03-26 00:34:02,760 - root - WARNING - There is something burning

所以我需要一种技术,它将使用键值对,并在日志文件中作为=并且不应该作为消息传递。它需要作为单独的参数传递。在


Tags: 模块文件apiisrootwhere条件something
1条回答
网友
1楼 · 发布于 2024-04-29 08:07:16

像这样。别恨我。在

>>> d = {'Where':'Kitchen', 'House':'Alice'}
>>> message = 'There is something burning. {0}'.format(
...     ', '.join(
...         ('{0}={1}'.format(k, v) for k, v in d.items())
...     )
... )
>>> message
'There is something burning. Where=Kitchen, House=Alice'

相关问题 更多 >