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

查看:64
本文介绍了在 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.

我建议坚持使用 logging 模块并在您的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天全站免登陆