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

C

CedricCicada

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.
 
V

Vinay Sajip

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
 
C

CedricCicada

Beautiful! Thank you very much!

One of the problems I was laboring under was that I did not know where
to go to find the official documentation. Thanks for that link too!

Rob
 
C

CedricCicada

Vinay (or anybody else),

Well, now that logging is working, how do I stop it from working?

I'm developing in PythonWin. When I run my test script, I get one
message. When I run it a second time, I get two, a third time gets me
three, and so on.

I feel like the Sorceror's Apprentice!

Rob
 
G

Gabriel Genellina

At said:
One of the problems I was laboring under was that I did not know where
to go to find the official documentation. Thanks for that link too!

You already have it installed; look into your python install
directory, under "doc"

From inside the interpreter, you can use help():

py> import logging.handlers
py> help(logging.handlers.NTEventLogHandler)
Help on class NTEventLogHandler in module logging.handlers:

class NTEventLogHandler(logging.Handler)
| A handler class which sends events to the NT Event Log. Adds a
| registry entry for the specified application name. If no dllname is
| provided, win32service.pyd (which contains some basic message [...]

Try help("logging"), help("modules logging"), help(any object), help("if")

And you can read the documentation online at http://www.python.org/doc/


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
V

Vinay Sajip

Vinay (or anybody else),

Well, now that logging is working, how do I stop it from working?

I'm developing in PythonWin. When I run my test script, I get one
message. When I run it a second time, I get two, a third time gets me
three, and so on.

I feel like the Sorceror's Apprentice!

Rob

The way to avoid this is to only call addHandler() once in your script.
PythonWin imports logging and keeps it in memory (something that
wouldn't happen if you ran the script repeatedly from the
command-line), so adding a handler every time results in multiple
handlers logging to the same event sink (e.g. NT Event Log).

So you need to use a flag to indicate whether initialisation has
already happened once, and avoid doing it again. I'm not sure of the
best way of doing this, since I don't know exactly how your app is set
up: if there's no more natural way, you could get a Logger instance and
see if it has any handlers added, before creating a new one and adding
it. This could be done by checking len(logger.handlers) == 0 for that
logger.

Best regards,


Vinay Sajip
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top