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
loggingmodule, the first requirement is for you to catch them all using except statements. Once you catch them, you then have to callLogger.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
stdoutandstderrto a file. Then, do atail -fto 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.
This allows you to use the neat
loggingmodule instead of having to rely on crappy output redirection.