I need to change the logger.exception default behavior to write to my logger with level=CRITICAL. It seems like something I can change but I haven't figured out how. My last resort is sys.excepthook but I don't want to use it because I do formatting in there.
thanks!
Logging and raising exceptions are two different things for two different purposes. Logs let you inspect what your program did after the fact. Raising exceptions has important effects on the program flow right now. Sometimes you want one, sometimes you want the other, sometimes you want both.
Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger. The arguments are interpreted as for debug().
except: accepts all exceptions, whereas. except Exception as e: only accepts exceptions that you're meant to catch. Here's an example of one that you're not meant to catch: >>> try: ...
getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.
You can log exceptions using CRITICAL
like this:
logger.critical('Message with %s', 'arguments', exc_info=True)
which will behave just like logger.exception
, only with a level of CRITICAL
rather than ERROR
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With