python logging, handling multiline log entries

  • Thread starter Jean-Michel Pichavant
  • Start date
J

Jean-Michel Pichavant

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
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top