7

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=
Bass
  • 195
  • 1
  • 2
  • 9

1 Answers1

10

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.

Phani
  • 3,267
  • 4
  • 25
  • 50
  • Thanks for quick feedback. The goal is to use logging module with more flexibility. I am trying to avoid using try/except on entire main, as I would like to have more control with handling exceptions. Is there a way to pipe runtime errors to both log and console via config file? – Bass Feb 25 '12 at 05:45
  • Any run time error would result in the whole program being halted anyway. So, unless you catch that exception there is nothing to be done. But, if you use `Logger.exception()` you will also get the traceback, so it is as good as getting the run-time error. This can be done even on main(). You will still be able to pin point the error because of traceback – Phani Feb 25 '12 at 05:51
  • Issue here is, I would like to throw some exceptions and recover from others. so using a blanket exception around main may not be a good idea. Also, the main method calls many other functions in other files, so the try/except might not cover them. The reason why I want to log runtime exceptions is because my app functions as a web service so if a user makes a request and does not get a response due to a runtime error on the producer, then I can troubleshoot with ease. – Bass Feb 25 '12 at 06:48
  • You can throw and catch exceptions at higher levels. Only if none of those exceptions are caught will this main except clause be invoked which otherwise means that your entire program will be halted anyway. As for using functions in other files exceptions of course do travel across functions so those too can be caught. But if the function starts as a separate thread then you'll have to use a general except clause right at the beginning of the new thread. – Phani Feb 25 '12 at 07:10
  • This now seems logical to me. Thumbs up Phani (after I accumulate enough points to do so). – Bass Feb 27 '12 at 20:57