473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2097
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Laszlo | last post by:
Hi I need a GL::Logger wich implements Class::Singleton to be a singleton and Log::Log4perl to can log amy message into a file. Here is the module source:...
3
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a...
1
by: Anonieko | last post by:
Global.asax? Use HttpModules Instead! In a previous post, I talked about HttpHandlers - an underused but incredibly useful feature of ASP.NET. Today I want to talk about HttpModules, which are...
11
by: D | last post by:
I have a winforms app that I'm reading some records from a datareader and writing them out to a file like so SqlDataReader dataReader = sqlCommand.ExecuteReader(); TextWriter textWriter = new...
3
by: seb | last post by:
Hi, I am writing to a file some basic information using the logging module. It is working but in the log file some line are printed several time. I had put some print debugging messages in the...
2
by: Doug Harding | last post by:
Hi all, As I have been trying to find a good method for tracing what is going on inside my javascript, I have seen many references to a logger called Lumberjack. It is supposed to be located at...
4
by: garyjefferson123 | last post by:
Suppose I have some sort of context variable that I want to appear in log messages. E.g.: logger = logging.getLogger("somelogger") class SomeOp: def __init__(self, ctx): self.ctx = ctx def...
2
by: DwBear75 | last post by:
I am hoping to find some simple examples of how to create a logger instance using smtphandler. I don't want to create a separate ini file. I just want to sent the smtphost, from, to right in the...
6
by: mjahabarsadiq | last post by:
Hi, I have written a java code to execute ant targets via Servlet. I have given the code below. import org.apache.tools.ant.*; import org.apache.catalina.ant.*; . . . .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.