Log the messages using \n
to separate the lines, as you've already considered, then write a custom Formatter
subclass which splits the message into separate lines, inserts the appropriate whitespace prefix for the subsequent lines and joins them back together again.
How to break information on an event for the logging
Question
I have been using the logging module in Python for a while. My doubt is not so much a technical one, but that I am not sure of the best practices with the messages. The issue is that sometimes an event happens and I want to dump information about it in the log, but a lot of information. I can build a single long string, and use it in an log.info() command. That is alright, except for the fact that the result is hardly human-readable. Introducing the character '\n' in the string indeed breaks the message into separate lines in the log file, but left-justified, it looks ugly because in the beginning of the logging formatter I am writing the date/time, user, etc.
On the other hand I can split the message into different calls to log.info(), but that makes it look as if they were different events.
EDIT: Additional information.
If I do different log calls, it looks like
2012-10-01 11:55:00,674 - SQLBOLPVWBA01 - WARNING | ---------- Warning 001: a test was not passed ----------
2012-10-01 11:55:00,694 - SQLBOLPVWBA01 - WARNING | File: \\b1snnasries01.SAN.CORP\D_8525_50_01\ValidacionModelos\Usuarios\Equity\testsValidationLibrary\XMLs\barrierMC\barrierMCbasket_0002_Q011388.xml
2012-10-01 11:55:00,704 - SQLBOLPVWBA01 - WARNING | Parametrization: configMatlabMC8192.m
2012-10-01 11:55:00,732 - SQLBOLPVWBA01 - WARNING | Expected: {'premium': 0.1302634916172996, 'stdErr': 0.0016369990559715}
2012-10-01 11:55:00,765 - SQLBOLPVWBA01 - WARNING | Calculated: {'premium': 0.13526349161729959, 'stdErr': 0.0016369990559715229}
What is the usual/best practice here?
I think I would like something like
2012-10-01 11:55:00,674 - SQLBOLPVWBA01 - WARNING | ---------- Warning 001: a test was not passed ----------
| File: \\b1snnasries01.SAN.CORP\D_8525_50_01\ValidacionModelos\Usuarios\Equity\testsValidationLibrary\XMLs\barrierMC\barrierMCbasket_0002_Q011388.xml
| Parametrization: configMatlabMC8192.m
| Expected: {'premium': 0.1302634916172996, 'stdErr': 0.0016369990559715}
| Calculated: {'premium': 0.13526349161729959, 'stdErr': 0.0016369990559715229}
Solution