I am trying to have the following functionality in my code:
develop = True
flagCurrently, I am using the Python3 logging
module, which (according to this) should have exactly that functionality built-in. The flag is then logging.raiseExceptions = True
.
However, I am not getting this to work in a MWE: the exception I am throwing does not get re-raised, whatever my setting of the flag is.
# mwe.py
import logging
import logging.config
if __name__ == '__main__':
# Configure logging and set flag to raise exceptions
logging.config.fileConfig('log.conf')
logging.raiseExceptions = True
logger = logging.getLogger('root')
logger.info('Started')
# Test whether exceptions get raised
try:
raise RuntimeError("Ooops.")
except RuntimeError:
try:
logger.exception('There was an oops.')
# which is the same as logger.error('...', exc_info=True)
except:
print("GOTCHA! Exception was re-raised.")
logger.info('Finished')
The corresponding config file:
# log.conf
[loggers]
keys=root
[handlers]
keys=consoleHandler
[formatters]
keys=consoleFormatter
[logger_root]
handlers=consoleHandler
level=DEBUG
[handler_consoleHandler]
class=logging.StreamHandler
formatter=consoleFormatter
args=(sys.stdout,)
[formatter_consoleFormatter]
format=%(filename)s (%(lineno)d) %(levelname)-8s - %(message)s
This produces the following output:
mwe.py (12) INFO - Started
mwe.py (19) ERROR - There was an oops.
Traceback (most recent call last):
File "mwe.py", line 16, in <module>
raise RuntimeError("Ooops.")
RuntimeError: Ooops.
mwe.py (24) INFO - Finished
Why am I not getting to the GOTCHA
part, although the default value of raiseExceptions
is True
and additionally I am also setting it to True
? What is wrong with my configuration?
Or do I have some big misunderstanding about the use of logging
for this purpose?
(Little bonus question: is there a way to configure the raiseException
flag in the log.conf
file?)
You got it wrong. This will not re-raise any custom exception. This is meant to change the default behaviour of logging module which is swallowing internal logging exceptions i.e. misconfigured logger trying to write to a file where it has no permissions to write. This will fail silently by default and setting logging.raiseException = True
will cause logger misconfiguration or any other problem within logging module to raise an exception which you would have to handle.
Now to the point of what you are trying to achieve. Having an exception traceback logged without suppressing the exception (letting it propagate) is the default behaviour. If logging is not configured the traceback goes to stderr. If logging is configured, it's written by logging handler to the desired location.
So basically in order to achieve your goal, you should not handle the exception at all. However if you know how to handle the exception (meaning you know what causes the exception) then normally you don't need a traceback to be logged.
If you insist you can still use logger.exception(...)
inside your except RuntimeError
clause and re-raise the exception with pure raise
:
try:
raise RuntimeError("Ooops.")
except RuntimeError:
logger.exception('There was an oops.')
raise
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