My first try using logger didn't work. Why?

Vinay Sajip vinay_sajip at yahoo.co.uk
Fri Jan 19 11:40:36 EST 2007


CedricCicada at gmail.com wrote:

> Greetings!
>
> I want to write messages into the Windows event log.  I found
> sevicemanager, but the source is always "Python Service", and I'd like
> to be a bit more descriptive.  Poking around on the Internet revealed
> the existence of the logging module.  It seems to have easily
> understood methods with the power I need.  So I tried it.  Here's my
> attempt:
>
>             logger = logging.getLogger("TahChung Part 1")
>             logger.setLevel(logging.INFO)
>             eventHandler = logging.NTEventLogHandler()
>             eventHandler.setlevel(logging.INFO)
>             formatter = logging.Formatter("%(message)s")
>             eventHandler.setFormatter(formatter)
>
>             logger.addHandler(eventHandler)
>             logger.error("This comes from the logger object.")
>
> I get no error messages from this, but I also don't get anything in my
> event log.  What am I doing wrong?
>
> By the way, my source of instructions for how to do this was:
> http://www.onlamp.com/pub/a/python/2005/06/02/logging.html
>
> Rob Richardson
> RAD-CON, Inc.

Well Rob,

Your first try didn't work because (based on your posted snippet) it
contained some errors.

The OnLAMP article was nice, but it's always best to go the the actual
documentation:

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

Where you will see that NTEventLogHandler (described on page
http://docs.python.org/lib/node418.html) requires an initialiser
argument. Also, "setlevel" is misspelt - it should be "setLevel". The
following slightly modified version of your script puts an entry in the
NT Event Log on my machine:

import logging, logging.handlers

logger = logging.getLogger("TahChung Part 1")
logger.setLevel(logging.INFO)
eventHandler = logging.handlers.NTEventLogHandler("TahChung")
eventHandler.setLevel(logging.INFO)
formatter = logging.Formatter("%(message)s")
eventHandler.setFormatter(formatter)

logger.addHandler(eventHandler)
logger.error("This comes from the logger object.")

Best regards,


Vinay Sajip




More information about the Python-list mailing list