Version 1 (modified by spascoe, 13 years ago) (diff)


Logging Best Practice

A simple logging class already exists at TI12-security/trunk/python/NDG/, however far greater functionality and flexibility can be achieved by useing the python  logging package. All developers should familiarise themselves with this package document: in particular read the introduction because understanding the class-level pages is difficult without it.


All modules that wish to use the logging infrastructure should include the following lines:

import logging

This ensures that the logger heirarchy will always reflect the package heirarchy. We would expect to end up with loggers such as "ndg.discovery.server" and "ndg.csml.parser".

Subsequent logging should be done by calling methods on the logger object, rather than the functions provided in the logging package. The latter will send messages to the root logger, thus bypassing any package-level filtering.

All scripts that import modules that user logging must ensure that at least one handler is installed into the logging heirarchy. The minimum code to do this is


# Or alternatively sending a message to the root logger
# will automatically install a handler'Initialising logging')

Scripts could also set more sophisticated logging requirements such as:


csmlLogger = logging.getLogger('ndg.csml')
csmlLogHandler = logging.FileHandler('./log/csml.log')

This would log all messages from the ndg.csml package (and sub-packages) to "./log/csml.log" and also log all ERROR and CRITICAL messages to sys.stdout (i.e. ERROR/CRITICAL messages from ndg.csml get logged twice).