Skip to content Skip to sidebar Skip to footer

Python: Custom Logging Across All Modules

Task I have a collection of scripts and I'd like them to produce unified logging messages with minimum alterations to modules doing logging the actual messages. I've written a smal

Solution 1:

If you want to change root logger you could just use getLogger() everywhere, with no arguments.

Regarding the instance setup only in the main module, you can instantiate your logger, add your own Handler, and use it in all the other submodules (as I did bellow).

I created a class that inherits the StreamHandler in custom_logger.py:

classMyHandler(logging.StreamHandler):def__init__(self):
        logging.StreamHandler.__init__(self)
        fmt = '%(asctime)s %(filename)-18s %(levelname)-8s: %(message)s'
        fmt_date = '%Y-%m-%dT%T%Z'
        formatter = logging.Formatter(fmt, fmt_date)
        self.setFormatter(formatter)

Then, in submodule.py, I put the getLogger after the imports and commented it in the methods:

import sys
import logging

log = logging.getLogger('root')

classSubClass(object):

    def__init__(self):
         log.info('message from SubClass / __init__')

    defSomeMethod(self):
        log.info('message from SubClass / SomeMethod')

Then, in app.py I created a Logger instance (that will be the same in all modules) and added my handler, which formats the output:

#!/usr/bin/python

import logging
import custom_logger
import submodule

log = logging.getLogger('root')
log.setLevel('DEBUG')
log.addHandler(custom_logger.MyHandler())

log.debug('debug message') 
log.info('info message')
log.warning('warning message')
log.error('error message')

a = submodule.SubClass() # this should produce a log message
a.SomeMethod()           # so should this

Output:

./app.py 
2013-04-08T15:20:05EEST app.py             DEBUG   : debug message2013-04-08T15:20:05EEST app.py             INFO    : info message2013-04-08T15:20:05EEST app.py             WARNING : warning message2013-04-08T15:20:05EEST app.py             ERROR   : error message2013-04-08T15:20:05EEST submodule.py       INFO    : message from SubClass / __init__2013-04-08T15:20:05EEST submodule.py       INFO    : message from SubClass / SomeMethod

Post a Comment for "Python: Custom Logging Across All Modules"