Extending Logger

Matimus mccredie at gmail.com
Fri Nov 7 14:23:05 EST 2008


> Yes but in the other hand :http://docs.python.org/library/logging.html#logger-objects
> "Note that Loggers are never instantiated directly, but always through
> the module-level function logging.getLogger(name)."

That is part of the power of the logging module. If you ask for a
logger of the same name in two different place you will get the same
object. That way you don't have to pass the logger instances around,
and it always has the same properties (same handlers attached, same
level, etc.). If you want to add functionality you getLoggerClass,
inherit from that, then setLoggerClass, and use the regular logging
facilities to instantiate them.

The only limitation here is that you can't change the constructor.
Well, it isn't useful to change the constructor. You can add a method
that does the same thing.

I can't help but feel that if you have a problem with this, you simply
aren't using the Logging module the way it was meant to be used.

Matt



More information about the Python-list mailing list