Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Traceback in Logging

Reply
Thread Tools

Traceback in Logging

 
 
Kottiyath
Guest
Posts: n/a
 
      01-06-2009
Hi all,
Is it possible to print traceback for all logging?
I have created something like this:

def trace():
import traceback
return ''.join(traceback.format_stack(limit=4)[1:])

And during every logging, append it -
self.logger.info('Data: %s, kwargs: %s Traceback: %s' %(data,
kwargs, trace()))

-->Result<--

2009-01-06 18:52:21,483 - test- INFO
---> Data:, kwargs: {}
Traceback: File "C:\test.py", line 48, in success
super(testObj, self).success(reply, **kwargs)
File "C:\test.py", line 87, in success
self.logger.info('Data: %s, kwargs: %s Traceback: %s' %(data,
kwargs, trace()))
File "C:\test.py", line 151, in trace
return ''.join(traceback.format_stack(limit=4)[1:])

This seems somewhat kludgy. Is it possible in logging mechanism itself
to provide traceback as default?

I use Python2.4, so LoggingAdapter is not there too.
 
Reply With Quote
 
 
 
 
Vinay Sajip
Guest
Posts: n/a
 
      01-06-2009
On Jan 6, 1:33 pm, Kottiyath <(E-Mail Removed)> wrote:
> This seems somewhat kludgy. Is it possible in logging mechanism itself
> to provide traceback as default?
>


No, because it's not a common use case to print tracebacks for every
logging call. There's support for adding traceback information in
exception handling, via use of the Logger.exception method in the
exception handling code.


> I use Python2.4, so LoggingAdapter is not there too.


If you need LoggingAdapter, you can always copy and paste the relevant
code from Python's SVN repository into your own application.

Regards,

Vinay Sajip
 
Reply With Quote
 
 
 
 
Kottiyath
Guest
Posts: n/a
 
      01-06-2009
I dont want the whole traceback. I just wanted to know where the log
was generated from i.e. which procedure and which line. I have 3/4
points in many procedures where I encounter a small error (not an
exception) and want to log it. So having individual names for each
looks to be somewhat verbose - esp since the application is >10K LOC.

This might be a good item to have in the logging system - along with
time and level, just proc name and the line number.

Thank you for the help.

Regards
K

Vinay Sajip wrote:
> On Jan 6, 1:33 pm, Kottiyath <(E-Mail Removed)> wrote:
> > This seems somewhat kludgy. Is it possible in logging mechanism itself
> > to provide traceback as default?
> >

>
> No, because it's not a common use case to print tracebacks for every
> logging call. There's support for adding traceback information in
> exception handling, via use of the Logger.exception method in the
> exception handling code.
>
>
> > I use Python2.4, so LoggingAdapter is not there too.

>
> If you need LoggingAdapter, you can always copy and paste the relevant
> code from Python's SVN repository into your own application.
>
> Regards,
>
> Vinay Sajip

 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      01-06-2009

Kottiyath> I dont want the whole traceback. I just wanted to know where
Kottiyath> the log was generated from i.e. which procedure and which
Kottiyath> line.

The asyncore standard module has an undocumented compact_traceback()
function:

#!/usr/bin/env python

import asyncore

def f():
1/0

def g():
f()

def h():
g()

try:
h()
except ZeroDivisionError:
print asyncore.compact_traceback()[3]

Output looks like this:

% python f.py
[f.py|<module>|15] [f.py|h|12] [f.py|g|9] [f.py|f|6]

Might be of interest.

--
Skip Montanaro - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://smontanaro.dyndns.org/
 
Reply With Quote
 
Vinay Sajip
Guest
Posts: n/a
 
      01-06-2009
On Jan 6, 4:17 pm, Kottiyath <(E-Mail Removed)> wrote:
> I dont want the whole traceback. I just wanted to know where the log
> was generated from i.e. which procedure and which line. I have 3/4
> points in many procedures where I encounter a small error (not an
> exception) and want to log it. So having individual names for each
> looks to be somewhat verbose - esp since the application is >10K LOC.
>


Don't the funcName and lineno arguments in the format string work for
you?

(See http://docs.python.org/library/logging.html#id1)

Regards,

Vinay Sajip
 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      01-06-2009
* Vinay Sajip (Tue, 6 Jan 2009 11:24:54 -0800 (PST))
> On Jan 6, 4:17 pm, Kottiyath <(E-Mail Removed)> wrote:
> > I dont want the whole traceback. I just wanted to know where the log
> > was generated from i.e. which procedure and which line. I have 3/4
> > points in many procedures where I encounter a small error (not an
> > exception) and want to log it. So having individual names for each
> > looks to be somewhat verbose - esp since the application is >10K LOC.
> >

>
> Don't the funcName and lineno arguments in the format string work for
> you?
>
> (See http://docs.python.org/library/logging.html#id1)


I don't think he's interested in the "line number where the logging call
was issued" but where the exception occurred.

Thorsten
 
Reply With Quote
 
Vinay Sajip
Guest
Posts: n/a
 
      01-07-2009
On Jan 6, 10:19 pm, Thorsten Kampe <(E-Mail Removed)> wrote:
> I don't think he's interested in the "line number where the logging call
> was issued" but where the exception occurred.


Well, he does say "all logging" and "every logging". And, as I
mentioned earlier, there is already support for tracebacks by using
the Logger.exception method in an exception handler.

Regards,

Vinay Sajip

 
Reply With Quote
 
Kottiyath
Guest
Posts: n/a
 
      01-08-2009
The issue is that I am on Python 2.4 which doesnt support func name.
I am using filename and lineno now. That does serve the purpose.
Thank you, I had not checked all the parameters.

Regards
K

Vinay Sajip wrote:
> On Jan 6, 4:17 pm, Kottiyath <(E-Mail Removed)> wrote:
> > I dont want the whole traceback. I just wanted to know where the log
> > was generated from i.e. which procedure and which line. I have 3/4
> > points in many procedures where I encounter a small error (not an
> > exception) and want to log it. So having individual names for each
> > looks to be somewhat verbose - esp since the application is >10K LOC.
> >

>
> Don't the funcName and lineno arguments in the format string work for
> you?
>
> (See http://docs.python.org/library/logging.html#id1)
>
> Regards,
>
> Vinay Sajip

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
Simulating logging.exception with another traceback Joan Miller Python 2 02-09-2010 10:06 AM
logging buffered vs. logging history Christian Roos Cisco 4 02-05-2006 10:55 PM
java.util.logging, where to put logging.properties? janne Java 0 09-10-2004 10:18 AM
[java.util.logging] logging only to _one_ file Stefan Siegl Java 0 08-27-2003 12:29 PM



Advertisments