4 ms·
You should, definitely. If you have subsystems where different abstractions or granularities might be useful, declare a logger for each of those. Document the
by jdunck 15y ago
You should, definitely.
If you have subsystems where different abstractions or granularities might be useful, declare a logger for each of those. Document them clearly. Make the top-level the name of your library. "libname.foo", "libname.bar", "libname.baz" are fine. Just use only one top-level namespace.
Use the logging levels appropriately. (Some libraries log everything as "warning", that's almost as bad as no logging.)
"Turn it off by default" - unless you are making a lib where performance is critical, logging overhead is noise. Your containing process will set up handlers as appropriate - they can turn off your output if they know what your loggers are.
For bonus points, provide logging configurations for "silent", "normal", and "debug".
I agree that the logging stdlib is a bit heavy for many uses. If your goal is to "just print a string", it can be a maddening array. OK:
def log(msg, kwargs):
import sys
print >> sys.stderr, msg % kwargs
But the logging module, used properly, is much more useful than that. I wish it had better docs and a better API, but it does have some nice features.