Logging multiple lines

Nikolaus Rath Nikolaus at rath.org
Wed Jun 17 04:22:31 CEST 2009


Hi,

Are there any best practices for handling multi-line log messages?

For example, the program,

    main = logging.getLogger()
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
    main.addHandler(handler)
    main.setLevel(logging.DEBUG)
    main.info("Starting")
    try:
        bla = 42/0
    except:
        main.exception("Oops...")

generates the log messages

2009-06-16 22:19:57,515 INFO Starting
2009-06-16 22:19:57,518 ERROR Oops...
Traceback (most recent call last):
  File "/home/nikratio/lib/EclipseWorkspace/playground/src/mytests.py", line 27, in <module>
    bla = 42/0
ZeroDivisionError: integer division or modulo by zero


which are a mess in any logfile because they make it really difficult to
parse.


How do you usually handle multi-line messages? Do you avoid them
completely (and therefore also the exception logging facilities provided
by logging)? Or is it possible to tweak the formatter so that it inserts
the prefix at the beginning of every line? 


Best,


   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Python-list mailing list