views:

45

answers:

1

Hello,

How can I set error reporting and warning outputs in Python like in PHP error_reporting(E_LEVEL)?

+1  A: 

A vaguely related option might be the setting of level in the logging module of the Python standard library, and I quote from Python's docs:

import logging
LOG_FILENAME = 'example.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

logging.debug('This message should go to the log file')

That level= determines which logging messages are emitted and which ones are filtered. However this only applies to errors (and other messages) emitted through logging module functions, not to (e.g) tracebacks resulting from exceptions; if you want to control the latter (what kinds of message come out when the process dies by propagating an exception), you can build something based on sys.excepthook, but your degrees of freedom will still be somewhat limited (in particular, after the reporting -- abundant or scarce as it may be -- the process will exit if an exception has propagated to that point).

Alex Martelli