Not really. It will flush each individual message, which is what you want.
FileHandler inherits from StreamHandler. StreamHandler calls self.flush() after every write() to the stream.
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.
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
Possible reason for my mistake
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?
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".
stderr
when tty, it tends to default to being unbuffered, unlike stdout which would usually be by default line-buffered. No idea aboutsystemd
/journald
, but I would assume it would make sense to change buffering of anystderr
capture/redirection to be the same (unbuffered). – Effacea
mode will basically whatever your system would do foropenat
call. py3.xa
is text mode, so you will get buffered text io (on not tty block, on tty line by default), the file handle bellowopenat
is same (except for|O_CLOEXEC
, "non-inheritable" fd essentially), but buffering comes fromio
module. – Efface