Custom logging function

Peter Otten __peter__ at web.de
Tue May 26 04:04:29 EDT 2020


zljubisic at gmail.com wrote:

> Hi,
> 
> I have a case in which I have to use custom function for logging.
> For example, all messages should go to stderr and end with '\r\n'.
> 
> Can I somehow use standard python logging module but send all message to
> stderr with '\r\n' line endings?

Isn't that the default for windows? For unix you can reconfigure stdout

sys.stdout.reconfigure(newline="\r\n")

or set the terminator attribute of the handler:

>>> import logging

Here we should build a stream handler explicitly -- but I'm lazy and modify 
the one created by basicConfig().

>>> logging.basicConfig()
>>> [handler] = logging.getLogger().handlers
>>> handler.terminator
'\n'
>>> logging.warning("Default line endings.")
WARNING:root:Default line endings.

Let's choose something fancy so that you can see the effect:

>>> handler.terminator = " -- here be dragons\r\n"
>>> logging.warning("Unusual line endings.")
WARNING:root:Unusual line endings. -- here be dragons




More information about the Python-list mailing list