在Python日志记录模块中禁止换行 [英] Suppress newline in Python logging module

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

问题描述

我正在尝试用Python的日志记录模块替换临时日志记录系统.我正在使用日志记录系统在一行上输出长任务的进度信息,因此您可以tail日志或在控制台中查看它.为此,我在日志记录功能上设置了一个标志,该标志可禁止显示该日志消息的换行符,并逐段构建该行.

I'm trying to replace an ad-hoc logging system with Python's logging module. I'm using the logging system to output progress information for a long task on a single line so you can tail the log or watch it in a console. I've done this by having a flag on my logging function which suppresses the newline for that log message and build the line piece by piece.

所有日志记录都是从单个线程完成的,因此没有序列化问题.

All the logging is done from a single thread so there's no serialisation issues.

是否可以使用Python的日志记录模块执行此操作?这是个好主意吗?

Is it possible to do this with Python's logging module? Is it a good idea?

推荐答案

让我们从您的最后一个问题开始:不,我不认为这是个好主意. IMO,从长远来看,这会损害日志文件的可读性.

Let's start with your last question: No, I do not believe it's a good idea. IMO, it hurts the readability of the logfile in the long run.

我建议坚持使用记录模块,并在其上使用'-f'选项您的"tail"命令以查看控制台的输出.您可能最终会使用 FileHandler .请注意,"delay"的默认参数为False,表示不会缓冲输出.

I suggest sticking with the logging module and using the '-f' option on your 'tail' command to watch the output from the console. You will probably end up using the FileHandler. Notice that the default argument for 'delay' is False meaning the output won't be buffered.

如果您真的需要取消换行符,我建议您创建自己的处理程序.

If you really needed to suppress newlines, I would recommend creating your own Handler.

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

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