Logging question

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Sep 23 13:49:12 CEST 2009

On Sep 23, 6:36 am, Gabor Urban <urbang... at gmail.com> wrote:
> Hi guys,
> I have embarassing problem using theloggingmodule. I would like to
> encapsulate the creation and setting up  of the logger in a class, but
> it does not seem working.
> Here are my relevant parts of the code:
> --

I'm not sure why you need to do this. Diez's reply tells you why you
don't see any output, but your code may also lead to other problems.
For example, if you create two LogClass instances with loggerName
values of "A" and "A.B", then any call to logger "A.B" will lead to
two messages in the log. That's because when a call to "A.B" is
handled, then it is passed to all handlers associated not only with
logger "A.B" but also "A" (its parent logger) and the root logger (its
grandparent). Since you have two FileHandlers configured (one for
"A.B" and one for "A"), the message will end up appearing in two files
(or the same file, if you used the same filename for both ClassLog

It's generally suspicious when you see someone trying to instantiate a
logger and adding a handler at the same time, as you're doing. The
reason this is a potential anti-pattern is that, other than for
trivial scripts, there isn't a natural one-to-one mapping between
loggers and handlers. Loggers (defined by their names, as in "A.B")
define areas of an application organized hierarchically (and answer
the question about a logging event, "Where did it happen?") whereas
handlers are about who's interested in those events, i.e. potential
log readers - they are generally organized according to the answer to
the question about a logging event, "Who wants to know?". In trivial
or command-line scripts, there's often just a one-to-one mapping (root
logger -> console) or one-to-two (root logger -> console and file) but
once your application gets more complex, then you usually have a good
few loggers (based on application areas) but just a few handlers (e.g.
one log file for everything, one log file for errors, console, and one
or two email handlers).


Vinay Sajip

More information about the Python-list mailing list