Thoughts on python logging
Jun. 15th, 2012 04:33 pmI'm increasingly impressed with python's excellent
By splitting generation from handling,
The only minor wrinkle I've encountered so far is the need to prevent a module's
logging module, especially the powerful way it combines with python's method of packaging modules.By splitting generation from handling,
logging makes it trivial to log messages in a module and delegate the process of actually printing them to a destination — file, console, network, whatever — to the calling application. By using a logger hierarchy that mirrors the standard python package naming conventions, per-module loggers can be initialised using __name__ and their output managed by adding a handler to the name of their parent module.The only minor wrinkle I've encountered so far is the need to prevent a module's
logger instance being exported when a higher level package does an import * on the package. Having originally thought about using a private __logger variable, I've realised that it's probably neater if I add an ___all__ = [ func1, func2, var1, var2 ] to the module. Not only does this prevent the logger from polluting the caller's namespace, but it also makes it easy to prevent other methods in the module from escaping into the wild.