logging module and threading
Vinay Sajip
vinay_sajip at yahoo.co.uk
Sat May 12 04:26:11 EDT 2007
On May 12, 1:53 am, Ross Boylan <r... at biostat.ucsf.edu> wrote:
>
> I'm also puzzled by how the logger hierarchy works. The docs say that
> everything that is logged by the kids is also logged by the parent.
> That would seem to defeat what I'm trying to do above, since the
> parent would get each logged event right away. However,logging.getLogger("a").error("test")
> produces only a single log message indicating an associated object of "a".
> The docs lead me to expect that I'd see one message from "a" and
> another from root.
>
> When I add handlers (e.g., FileHandlers) I do get the message recorded
> by each.
>
> Can anyone explain what's going on?
>
The Logger hierarchy works, in a nutshell, as follows: events passed
to a logger propagate up the hierarchy until either the root, or a
logger whose propagate attribute is set to a false value, is reached.
At each step (i.e. logger) in the propagation, any handlers attached
to that logger are offered the chance to handle the message - which
means different things depending on the handler. If you add e.g. a
FileHandler to the root logger and another to a logger named "a.b.c"
and then log something to "a.b.c", then two handlers will each handle
the message, leading to multiple output of the same event. You also
don't need to buffer up messages in different threads - if your
messages contain both the log event time and the thread id (which can
be achieved by having %(asctime)s or %(relativeCreated)d and %
(thread)d or %(threadName)s in your format string), then you can
simply sort the output to group your messages together. (And since
threads sometimes interact, it's often useful to see the raw output in
time order.) If you do want to buffer events, MemoryHandler will do
the trick. And it takes a target handler rather than logger, because
it's acting as a buffer for another handler, for events which have
already been entered into the system.
If you're new to logging, you need to separate the ideas behind
loggers and handlers in the scheme of things. Loggers are things which
application developers use to register events of interest in their
application, with an indication of importance (level) and an ability
to filter on that. Handlers are things used by developers or admins to
send information about the events to different audiences - e.g.
critical errors might be emailed to someone while less critical errors
are sent to console or log files.
Best regards,
Vinay Sajip
More information about the Python-list
mailing list