python logging.FileHandler是否默认使用块缓冲? [英] Does python logging.FileHandler use block buffering by default?

查看:379
本文介绍了python logging.FileHandler是否默认使用块缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

logging处理程序类具有flush()方法.

The logging handler classes have a flush() method.

并查看代码,<调用open()时c1>没有通过特定的缓冲模式.因此,当您写入日志文件时,将使用默认的块大小对其进行缓冲.

And looking at the code, logging.FileHandler does not pass a specific buffering mode when calling open(). Therefore when you write to a log file, it will be buffered using a default block size.

对吗?

这让我感到惊讶,因为当我管理自己的系统时,我习惯于实时查看日志文件(或

It surprises me, because when I manage my own system, I am used to watching log files as a live (or near-live) view on the system. For this use case, line-buffering is desired. Also, traditional syslog() to a logging daemon does not buffer messages.

我对Python的最新版本(2.7和3.7)感兴趣.

I am interested in recent versions of Python: both 2.7 and 3.7.

(也许是最近的历史.任何链接到所建议补丁的答案都是有力的竞争者:-).这里有些观点可能是有意义的,因为我经验不足.

(And maybe recent history. Any answer which links to a proposed patch will be a strong contender :-). Some opinions might be relevant here, because I am not very experienced).

推荐答案

不是.它将刷新每个单独的消息,这就是您想要的.

Not really. It will flush each individual message, which is what you want.

FileHandler继承自StreamHandler.在每次对流执行write()之后,StreamHandler都会调用self.flush().

FileHandler inherits from StreamHandler. StreamHandler calls self.flush() after every write() to the stream.

如果您查看logging.MemoryHandler,则flush()方法开始变得更有意义.对于要添加缓冲的程序,MemoryHandler允许包装另一个处理程序,并缓冲一定数量的消息.超过设置的严重性级别的消息也将立即刷新. logging不包含一个处理程序,该处理程序每​​秒钟左右会自动刷新一次,但是您始终可以自己编写一个.

The flush() method starts to make more sense if you look at logging.MemoryHandler. For programs which want to add buffering, MemoryHandler allows wrapping another handler, and buffering a set number of messages. It will also flush immediately on messages above a set severity level. logging does not include a handler which automatically flushes every second or so, but you could always write one yourself.

如果您的程序作为systemd服务运行并且您登录到stderr,则在StreamHandler 中的flush调用也意味着它可以执行您想要的操作.在这种情况下,Python 3需要刷新.当不是TTY时,Python 3当前对stderr使用块缓冲.参见关于Python问题13597的讨论

The flush calls in StreamHandler also mean that it does what you want, if your program is run as a systemd service and you log to stderr. Python 3 requires flushes in this case. Python 3 currently uses block buffering for stderr when it is not a TTY. See discussion on Python issue 13597

我认为我对StreamHandler代码感到困惑.如果用户永远不需要调用flush()方法,为什么StreamHandler会定义一个非空的,公开记录的实现?

I think I was confused by the StreamHandler code. If the user never needed to call the flush() method, why would StreamHandler define a non-empty, publicly documented implementation?

我想我承担了太多,并且我不允许在这里使用继承(argh).例如.基本的Handler类有一个空的flush()方法,但是StreamHandler不想继承它,因为它有一个奇怪的文档字符串此版本不执行任何操作,并且打算由子类实现."

I think I was assuming too much, and I did not allow for how inheritance (argh) is used here. E.g. the base Handler class has an empty flush() method, but StreamHandler does not want to inherit that because it has a weird docstring "This version does nothing and is intended to be implemented by subclasses".

这篇关于python logging.FileHandler是否默认使用块缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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