Ryan
Ryan

Reputation: 39

how to redirect traceback.print_exc to logging

I am using eventlet with Python to spawn some threads. I am using eventlet.spawn_n(). I can see from the implementation here that it prints a stack trace if the method you run throws an exception: https://github.com/eventlet/eventlet/blob/79292bd16a6a1384d8c3d11bcefa40eb53bbeae4/eventlet/greenpool.py#L93

This stack trace is going to stderr. But the logging for everything else in my program goes to a file using WatchedFileHandler. This causes the stack traces to not be shown in the the log file where the rest of the logging for my application takes place which is causing some problems.

Is there a way to get the stack traces from eventlet to print to the same file that the rest of my application logs to rather than printing to stderr?

Here is the logging configuration I currently use:

[loggers]
keys = root

[handlers]
keys = watchedfile

[formatters]
keys = default

[logger_root]
level = INFO
handlers = watchedfile

[handler_watchedfile]
class = handlers.WatchedFileHandler
args = ('my_log.txt',)
formatter = default

[formatter_default]
format = %(asctime)s %(levelname)s [%(name)s] %(pathname)s:%(funcName)s:%(lineno)s %(message)s

Upvotes: 1

Views: 1231

Answers (1)

temoto
temoto

Reputation: 5577

The key thing here is what you observe happens with eventlet.greenpool.DEBUG enabled (by default). And it prints traceback of uncaught exceptions. Things you should've wrapped in try/except. Without that safeguard debug print_exc, you'd have errors that are just lost. Forgetting about errors is bad.

As you pointed in code, it uses traceback.print_exc(), so question could be rewritten w/o Eventlet to "how to redirect traceback.print_exc to logging". Or even "how to redirect sys.stderr to logging". You may find better solutions with that.

Redirects aside, your options:

  • just use GreenPool.spawn(), it reraises exceptions in thread that gets results (usually in form of pool.waitall())
  • use stderr handler, write to file using external program, such as syslog, it solves more issues than just reopening rotated logs
  • catch errors in spawned function and use logging

Upvotes: 1

Related Questions