logging: StreamHandler’s newline terminator now configurable

V

Vinay Sajip

When StreamHandler writes a formatted log message to its stream, it
adds a newline terminator. This behaviour is inherited by FileHandler
and the other classes which derive from it (such as the rotating file
handlers).

For most people, that’s what they want, since they get log messages on
separate lines without having to explicitly code newlines in their log
messages. However, some people want the flexibility to not have this
newline automatically appended to logged messages.

Starting with Python 3.2, the message terminator will be configurable.
This has been done by adding a terminator attribute to StreamHandler,
which when emitting an event now writes the formatted message to its
stream first, and then writes the terminator. If you don’t want
newline termination for a handler, just set the handler instance’s
terminator attribute to the empty string.

There’s a small chance that this approach will cause unexpected
behaviour – in the unlikely instance that someone is setting a
terminator attribute on an a StreamHandler or one of its subclasses
for their own, unrelated purposes. Other than that, the new behaviour
should be backwards compatible.

If this change is likely to adversely affect you for any reason,
please let me know, by replying to this post. The change has already
been checked into the py3k branch, and you are welcome to try it out.

Regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top