469,628 Members | 1,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

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

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/2...2/logging.html

Rob Richardson
RAD-CON, Inc.

Jan 19 '07 #1
5 1894
Ce**********@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/2...2/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

Jan 19 '07 #2
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

Jan 19 '07 #3
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

Jan 19 '07 #4
At Friday 19/1/2007 15:59, Ce**********@gmail.com wrote:
>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():

pyimport logging.handlers
pyhelp(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

Jan 19 '07 #5
Ce**********@gmail.com wrote:
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

Jan 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Laszlo | last post: by
3 posts views Thread by rh0dium | last post: by
1 post views Thread by Anonieko | last post: by
3 posts views Thread by seb | last post: by
2 posts views Thread by Doug Harding | last post: by
4 posts views Thread by garyjefferson123 | last post: by
2 posts views Thread by DwBear75 | last post: by
6 posts views Thread by mjahabarsadiq | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.