The script below is writing all errors to both log file and console, except for the raised exception which is only written on console and not on log. How to I get it to write the raised exception to log, or any runtime exception for that matter? Thanks.
import os
import sys
import logging
import logging.config
class Main(object):
@staticmethod
def main():
logging.config.fileConfig("logging.conf")
logging.debug("1")
logging.info("2")
logging.warn("3")
logging.error("4")
logging.critical("5")
raise Exception("test")
if __name__ == "__main__":
Main.main()
import logging
import logging.config
logging.config.fileConfig('logging.conf')
# create logger
logger = logging.getLogger('simpleExample')
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
raise Exception("Exception raised")
Config file:
[loggers]
keys=root,simpleExample
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0
[handler_fileHandler]
formatter=simpleFormatter
args=('error.log')
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
In order to have all the errors captured using the logging module, the first requirement is for you to catch them all using except statements. Once you catch them, you then have to call Logger.exception() or another suitable function according to the level of error.
If you cannot catch all exceptions beforehand, your best bet would be to redirect stdout and stderr to a file. Then, do a tail -f to simulate a console output. Anyway, one uncaught exception will result in your program execution being stopped.
But, I would prefer trying to catch all exceptions even if that means having to do something like this.
try:
Main.main()
except Exception as e:
logging.Exception("Unexpected exception! %s",e)
This allows you to use the neat logging module instead of having to rely on crappy output redirection.
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