Python中的多行记录 [英] Multi-line logging in Python

查看:42
本文介绍了Python中的多行记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.3.5和日志记录模块将信息记录到本地文件(来自不同线程).在某些情况下,我想输出一些其他信息,而又不确切知道该信息是什么(例如,它可能是一行文本或一则字典).

I'm using Python 3.3.5 and the logging module to log information to a local file (from different threads). There are cases where I'd like to output some additional information, without knowing exactly what that information will be (e.g. it might be one single line of text or a dict).

我想做的是在写入日志记录之后,将此附加信息添加到我的日志文件中.此外,仅在日志级别为错误(或更高)时才需要附加信息.

What I'd like to do is add this additional information to my log file, after the log record has been written. Furthermore, the additional info is only necessary when the log level is error (or higher).

理想情况下,它看起来像:

Ideally, it would look something like:

2014-04-08 12:24:01 - INFO     - CPU load not exceeded
2014-04-08 12:24:26 - INFO     - Service is running
2014-04-08 12:24:34 - ERROR    - Could not find any active server processes
Additional information, might be several lines.
Dict structured information would be written as follows:
key1=value1
key2=value2
2014-04-08 12:25:16 - INFO     - Database is responding

编写自定义日志格式化程序的时间很短,我找不到满足我要求的内容.我已经阅读了有关过滤器和上下文的信息,但这似乎并不是一个很好的选择.

Short of writing a custom log formatter, I couldn't find much which would fit my requirements. I've read about filters and contexts, but again this doesn't seem like a good match.

或者,我可以使用标准I/O写入文件,但是大多数功能已经存在于日志记录"模块中,而且它是线程安全的.

Alternatively, I could just write to a file using the standard I/O, but most of the functionality already exists in the Logging module, and moreover it's thread-safe.

任何输入将不胜感激.如果确实需要自定义日志格式化程序,那么任何从何处开始的指针都将是奇妙的.

Any input would be greatly appreciated. If a custom log formatter is indeed necessary, any pointers on where to start would be fantastic.

推荐答案

我只是在输出文本中添加 \ n 符号.

I just add \n symbols to the output text.

这篇关于Python中的多行记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆