Logging Stacktrace To File

O

Olaf Meding

In case of an exception, how could I log an exception to a file on disk?

Much appreciate your help, thank you.

Olaf
 
J

Jorge Godoy

In case of an exception, how could I log an exception to a file on disk?

I think you'll have to catch the exception and then log it to the file.
After that, you should raise the exception again.

But I think that if any other part of the program deals with such an
exception before you do, then you might miss it.
 
O

Olaf Meding

... have to catch the exception and then log it to the file.

Right. I do also know how to open a file and write to it.

But how do I get at the stack trace? I would like to have it look the same
in the file as what gets printed to the screen.

Thanks again.

Olaf
 
P

Peter Hansen

Olaf said:
Right. I do also know how to open a file and write to it.

But how do I get at the stack trace? I would like to have it look the same
in the file as what gets printed to the screen.

You're looking for the standard module "traceback", plus possibly
a call to sys.exc_info()...

The Cookbook has some helpful examples, probably more complex than
what you need:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65332

-Peter
 
P

Peter Otten

Olaf said:
Right. I do also know how to open a file and write to it.

But how do I get at the stack trace? I would like to have it look the
same in the file as what gets printed to the screen.

A quick and dirty approach to deal with _uncaught_ exceptions (untested):
.... sys.stderr = file("tmp.txt", "a")
.... sys.__excepthook__(klass, inst, tb)
.... sys.stderr.close()
.... sys.stderr = sys.__stderr__
....Traceback (most recent call last):
$ cat tmp.txt
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Exception: so what

Peter
 
B

BW Glitch

Olaf said:
Right. I do also know how to open a file and write to it.

But how do I get at the stack trace? I would like to have it look the same
in the file as what gets printed to the screen.

The traceback module provides you with that hook. The function is
traceback.print_exc and you can pass a file-like object in the second
parameter.

--
Glitch

http://andres980.tripod.com/tf.html

"... You WIN, you depraved wad a' stinkin' slag."
"Cultivated as always, even in defeat."
-- Rattrap and Megatron, "Fallen Comrades"
 
S

Steve Holden

Olaf said:
In case of an exception, how could I log an exception to a file on disk?

Much appreciate your help, thank you.

Olaf
Here's what I use, modulo a certain amount of mangling by Mozilla, which
I am still trying to tame:

def hook(et, ev, eb):
import traceback
log("\n ".join (["Error occurred: traceback
follows"]+list(traceback.format_exception(et, ev, eb))))

sys.excepthook = hook

This works very well, but you'll need to supply your own "log()"
function. Shouldn't be too hard ...

regards
Steve
 

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
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top