logging multiple messages

D

Daniel

I was fighting with a problem all day that was producing multiple
messages in my logging output. The problem was related to the fact
that I was defining logging handlers multiple times. I found the
following posting from a few years ago that related to my problem:

http://groups.google.com/group/comp...f5ba68721f?lnk=gst&q=logging#44f16cf5ba68721f

What I did was the following:

def initLogging(loggingArea):
# create/get logger
logger = logging.getLogger("mylogger")
logger.setLevel(logging.DEBUG)

# setup Handlers
if len(logger.handlers) == 0:
# create handlers only if there are none
sh = logging.StreamHandler()
# now add the handlers
logger.addHandler(sh)

If the logger already has handlers nothing happens, otherwise they're
created and added to the logger. Now I can call initLogging
('mylogarea') whereever I need to without getting multiple messages.

If you know of a better way to do this let me know...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top