I wrote some code to handle and log exceptions in my application.
It works well, but it produces double output for each exception.
How can I fix this?
Here's the pared-down code:
----- main.py
import exceptionLogger
import doStuff
exlog = exceptionLogger.exceptionLogger()
stuff = doStuff.doStuff()
try:
stuff.doit()
except Exception,msg:
exlog.logException()
----- exceptionLogger.py
import traceback
import logging
import os
class exceptionLogger:
loggingLevel = ""
logger = None
def __init__(self):
self.logger = logging.getLogger("foo")
self.loggingLevel = "DEBUG"
if self.loggingLevel == "DEBUG":
self.logger.setLevel(logging.DEBUG)
handler = logging.FileHandler("error.txt")
format = "%(asctime)s %(levelname)-8s %(message)s"
formatter = logging.Formatter(format)
handler.setFormatter(formatter)
self.logger.addHandler(handler)
def logMsg(self,message):
try:
if self.loggingLevel == "DEBUG":
self.logger.debug(message)
except:
self.stopLogging()
def logException(self):
"""Write an exception traceback to the logfile."""
# save the stack trace to a file and then copy the contents of that
# file to the log. (traceback.format_exc would allow us to avoid
# using a temporary file, but it is not available in python 2.3)
TMPFILE = os.tmpfile()
traceback.print_exc(None, TMPFILE)
TMPFILE.flush()
self.logMsg(" ************** EXCEPTION BEGINS **************")
self.logMsgFromFile(TMPFILE)
self.logMsg(" ************** EXCEPTION ENDS **************")
TMPFILE.close()
def logMsgFromFile(self,file):
"""Read the contents of a file into a string and log the string."""
file.seek(0)
msg = ""
for line in file:
msg = msg + line
self.logMsg(msg)
def stopLogging(self):
logging.shutdown()
----- doStuff.py
import exceptionLogger
class doStuff:
def doit(self):
exlog = exceptionLogger.exceptionLogger()
try:
raise ValueError, "hi there"
except Exception:
exlog.logException()
And here's the contents of error.txt:
----- errror.txt
2009-09-24 14:22:23,288 DEBUG ************** EXCEPTION BEGINS **************
2009-09-24 14:22:23,288 DEBUG ************** EXCEPTION BEGINS **************
2009-09-24 14:22:23,288 DEBUG Traceback (most recent call last):
File "/home/gordonj/exception/doStuff.py", line 8, in doit
raise ValueError, "hi there"
ValueError: hi there
2009-09-24 14:22:23,288 DEBUG Traceback (most recent call last):
File "/home/gordonj/exception/doStuff.py", line 8, in doit
raise ValueError, "hi there"
ValueError: hi there
2009-09-24 14:22:23,288 DEBUG ************** EXCEPTION ENDS **************
2009-09-24 14:22:23,288 DEBUG ************** EXCEPTION ENDS **************
As you can see, all of the output is doubled. Header lines, footer lines,
and body text.
Why is this happening? I suspect it's because I'm declaring two instances
of the exceptionLogger class, which ends up calling logger.addHandler()
twice. Is that right?
What would be a better way to handle this?
Thanks.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
|
|
0
|
|
|
|
Reply
|
gordon16 (617)
|
9/24/2009 7:43:14 PM |
|
On Sep 24, 8:43=A0pm, John Gordon <gor...@panix.com> wrote:
> Why is this happening? =A0I suspect it's because I'm declaring two instan=
ces
> of the exceptionLogger class, which ends up calling logger.addHandler()
> twice. =A0Is that right?
>
Yes, that's why you get duplicated lines in the log.
> What would be a better way to handle this?
I'm not sure why you need all the code you've posted. The logging
package allows you to add tracebacks to your logs by using the
exception() method, which logs an ERROR with a traceback and is
specifically intended for use from within exception handlers. All that
stuff with temporary files seems completely unnecessary, even with
Python 2.3.
In general, avoid adding handlers more than once - which you are
almost guaranteed to not avoid if you do this kind of processing in a
constructor. If you write your applications without using the
exceptionLogger class (not really needed, since logging exceptions
with tracebacks is part and parcel of the logging package's
functionality since its introduction with Python 2.3), what
functionality do you lose?
Regards,
Vinay Sajip
|
|
0
|
|
|
|
Reply
|
vinay_sajip (335)
|
9/25/2009 8:23:04 PM
|
|
In <6bce12c3-f2d9-450c-89ee-afa4f21d50c8@h30g2000vbr.googlegroups.com> Vinay Sajip <vinay_sajip@yahoo.co.uk> writes:
> The logging package allows you to add tracebacks to your logs by using
> the exception() method, which logs an ERROR with a traceback and is
> specifically intended for use from within exception handlers. All that
> stuff with temporary files seems completely unnecessary, even with Python
> 2.3.
I didn't know about the exception() method. Thanks!
> In general, avoid adding handlers more than once - which you are
> almost guaranteed to not avoid if you do this kind of processing in a
> constructor. If you write your applications without using the
> exceptionLogger class (not really needed, since logging exceptions
> with tracebacks is part and parcel of the logging package's
> functionality since its introduction with Python 2.3), what
> functionality do you lose?
I'm trying to encapsulate all the setup work that must be done before any
actual logging occurs: setting the debugging level, choosing the output
filename, declaring a format string, adding the handler, etc.
If I didn't do all that in a class, where would I do it?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
|
|
0
|
|
|
|
Reply
|
gordon16 (617)
|
9/28/2009 8:38:49 PM
|
|
On Mon, Sep 28, 2009 at 4:38 PM, John Gordon <gordon@panix.com> wrote:
> In <6bce12c3-f2d9-450c-89ee-afa4f21d50c8@h30g2000vbr.googlegroups.com> Vi=
nay Sajip <vinay_sajip@yahoo.co.uk> writes:
>
>> The logging package allows you to add tracebacks to your logs by using
>> the exception() method, which logs an ERROR with a traceback and is
>> specifically intended for use from within exception handlers. All that
>> stuff with temporary files seems completely unnecessary, even with Pytho=
n
>> 2.3.
>
> I didn't know about the exception() method. =A0Thanks!
>
>> In general, avoid adding handlers more than once - which you are
>> almost guaranteed to not avoid if you do this kind of processing in a
>> constructor. If you write your applications without using the
>> exceptionLogger class (not really needed, since logging exceptions
>> with tracebacks is part and parcel of the logging package's
>> functionality since its introduction with Python 2.3), what
>> functionality do you lose?
>
> I'm trying to encapsulate all the setup work that must be done before any
> actual logging occurs: setting the debugging level, choosing the output
> filename, declaring a format string, adding the handler, etc.
>
> If I didn't do all that in a class, where would I do it?
Put it in a module. :]
> --
> John Gordon =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 A is for Amy, who fell do=
wn the stairs
> gordon@panix.com =A0 =A0 =A0 =A0 =A0 =A0 =A0B is for Basil, assaulted by =
bears
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Edward =
Gorey, "The Gashlycrumb Tinies"
>
|
|
0
|
|
|
|
Reply
|
sajmikins (174)
|
9/28/2009 11:01:10 PM
|
|
On Sep 28, 9:38=A0pm, John Gordon <gor...@panix.com> wrote:
>
> If I didn't do all that in a class, where would I do it?
>
You could, for example, use the basicConfig() function to do it all
for you.
import logging
logging.basicConfig(filename=3D'/path/to/my/log',level=3Dlogging.DEBUG)
logging.debug('This message should go to the log file')
Here's the documentation link for simple examples:
http://docs.python.org/library/logging.html#simple-examples
Here's the link for basicConfig:
http://docs.python.org/library/logging.html#logging.basicConfig
Regards,
Vinay Sajip
|
|
0
|
|
|
|
Reply
|
vinay_sajip (335)
|
9/29/2009 6:53:35 AM
|
|
Vinay Sajip wrote:
> I'm not sure why you need all the code you've posted. The logging
> package allows you to add tracebacks to your logs by using the
> exception() method, which logs an ERROR with a traceback and is
> specifically intended for use from within exception handlers.
You can also use the exc_info=True parameter with any logging method...
Chris
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
|
|
0
|
|
|
|
Reply
|
chris2154 (203)
|
9/30/2009 1:53:08 PM
|
|
John Gordon wrote:
> If I didn't do all that in a class, where would I do it?
I find the configureLoggers method of ZConfig most convenient for this:
http://pypi.python.org/pypi/ZConfig
cheers,
Chris
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
|
|
0
|
|
|
|
Reply
|
chris2154 (203)
|
9/30/2009 1:54:29 PM
|
|
|
6 Replies
14 Views
(page loaded in 0.107 seconds)
|