Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > logging module and threading

Reply
Thread Tools

logging module and threading

 
 
Ross Boylan
Guest
Posts: n/a
 
      05-12-2007
I would like my different threads to log without stepping on each
other.

Past advice on this list (that I've found) mostly says to send the
messages to a Queue. That would work, but bypasses the logging
module's facilities.

The logging module itself is "thread-safe", but I think that just
means that individual output is protected. If I have, in temporarly
sequence:
thread 1: warning("A")
thread 2: info("something")
thread 1: warning("B")
then I think I'll get them output in this order. It's thread-safe in
that the log will not end up with an entry like
A
some
B
thing
(I think). But I want to get, for example,
A
B
something

What I would like is for each thread to emit a chunk of log messages
when it finishes a unit of work.

It looks as if I might be able to use a MemoryHandler to accumulate
the log locally and then flush it into the main log (I'd like to send
it to the main logger, but it looks as if I must send it to a specific
handler). Would something like the following work?

class MyThread (threading.Thread):
def __init__(self):
# do I need the next line?
threading.Thread.__init__(self)
self._log = logging.getLogger(self.getName())
# flush into main log
self._log = logging.MemoryHandler(9999999,
, # default flushlevel
logging.getLogger().handlers[1] )

def run(self):
j = getjob()
while j:
# do stuff
# log like this
self._log.info("some message")
# when done
self._log.flush()
j = getjob()


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?

Thanks.
Ross Boylan



 
Reply With Quote
 
 
 
 
Vinay Sajip
Guest
Posts: n/a
 
      05-12-2007
On May 12, 1:53 am, Ross Boylan <r...@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

 
Reply With Quote
 
 
 
 
James T. Dennis
Guest
Posts: n/a
 
      06-13-2007
Ross Boylan <> wrote:
> I would like my different threads to log without stepping on each
> other.


> Past advice on this list (that I've found) mostly says to send the
> messages to a Queue. That would work, but bypasses the logging
> module's facilities.


> The logging module itself is "thread-safe", but I think that just
> means that individual output is protected. If I have, in temporarly
> sequence:
> thread 1: warning("A")
> thread 2: info("something")
> thread 1: warning("B")
> then I think I'll get them output in this order. It's thread-safe in
> that the log will not end up with an entry like
> A
> some
> B
> thing
> (I think). But I want to get, for example,
> A
> B
> something


> What I would like is for each thread to emit a chunk of log messages
> when it finishes a unit of work.



This sounds like a job for the Queue class/module to me.
Could you create a Queue such that all your worker threads
are producers to it and you have one dedicated thread as a
consumer that relays log entries from the Queue into your loggers?



--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

 
Reply With Quote
 
Vinay Sajip
Guest
Posts: n/a
 
      06-13-2007
On Jun 13, 1:28 am, "James T. Dennis" <jades...@idiom.com> wrote:
> This sounds like a job for the Queue class/module to me.
> Could you create a Queue such that all your worker threads
> are producers to it and you have one dedicated thread as a
> consumer that relays log entries from the Queue into your loggers?


Or, use a SocketHandler to serialize the events over a socket, and de-
mux them on the receiving end. The docs have an example:

http://docs.python.org/lib/network-logging.html

Regards,

Vinay Sajip

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
threading in PyQt vs threading in standard library Steven Woody Python 0 01-09-2009 07:15 AM
Cooperative threading preemptive threading - a bit confused failure_to@yahoo.co.uk Java 9 12-29-2007 01:10 AM
Re: Logging to a file and closing it again properly (logging module) Christoph Haas Python 1 06-14-2006 08:47 AM
Logging to a file and closing it again properly (logging module) Christoph Haas Python 0 06-12-2006 09:58 PM
Passing a log handle to a module? Help needed with logging module and rh0dium Python 2 08-04-2005 02:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57