Extending Logger

Thomas Christensen thomasc at thomaschristensen.org
Fri Nov 7 00:17:15 CET 2008


polettog at gmail.com writes:

> I took a look to the logging module which was quite sexy at a first
> sight, but then i finally realized the following : the Logger class
> can't be extended since a Logger is created only with getLogger (the
> subclass can't call/shouldn't call Logger.__init__()).
> So, did i miss something? or maybe there's no need to extend the
> Logger class?

Taken from http://docs.python.org/library/logging.html:

logging.getLoggerClass()

    Return either the standard Logger class, or the last class passed to
    setLoggerClass(). This function may be called from within a new
    class definition, to ensure that installing a customised Logger
    class will not undo customisations already applied by other
    code. For example:

    class MyLogger(logging.getLoggerClass()):
        # ... override behaviour here

logging.setLoggerClass(klass)

    Tells the logging system to use the class klass when instantiating a
    logger. The class should define __init__() such that only a name
    argument is required, and the __init__() should call
    Logger.__init__(). This function is typically called before any
    loggers are instantiated by applications which need to use custom
    logger behavior.


		Thomas




More information about the Python-list mailing list