Logging

Setup

Create a module to manage the configuration:

import logging

LOGGING_FORMAT = '%(asctime)s %(levelname)-5s %(module)s %(funcName)s %(message)s'

logging.basicConfig(
    level = logging.DEBUG,
    format = LOGGING_FORMAT,
    datefmt = '%H:%M:%S',
    filename = 'logger.log',
    filemode = 'w'
)

Note:

  • See Formatter Objects.

  • See LOGGING_FORMAT

  • Remove the filemode if you want to append to the file.

  • If (for example) you name this module, logging_config.py, your main module (or unit test), will need to import logging_config to activate the configuration.

  • python version 2.4, didn’t seem to recognise the funcName parameter.

In the other modules of your application, import the configuration module and start logging:

import logging

logger = logging.getLogger(__name__)

logger.debug('some log text')

See Advanced Logging Tutorial for details about using getLogger.