R
Ross Boylan
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
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