python logging, handling multiline log entries

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Nov 4 07:34:08 EDT 2010


Hi python fellows,

I'm looking to do the following :

import logging

l = logging.getLogger('aHeader')
l.handlers = []
l.addHandler(logging.StreamHandler())
l.handlers[-1].setFormatter(logging.Formatter("%(asctime)s - %(name)s - 
%(message)s"))
l.error('1st line\n2nd line')

output:
2010-11-04 12:22:05,593 - aHeader - 1st line
2nd line



I'd like to get something like:
2010-11-04 12:22:05,593 - aHeader - 1st line
                                    2nd line

I don't want to get the header on every line, otherwise I would just 
need to log every lines.

Is there a simple way of doing that? anyone already gave a though on that ?

The main problem I'm facing is that I cannot alter the log entry itself, 
I don't know the size of the header and I think it would be 
anti-pattern. My guess is that it should be handled in  the formatter, 
overriding the format method, but how do you get the header size (i.e. 
the indentation size).

So if anyone has a piece of code doing that, feel free to point it to me.

Cheers,

JM






More information about the Python-list mailing list