How do I format error message and stack when logging an Exception in python3 logging setup with dictconfig?

Viewed 386

I'm using logging.config.dictconfig() and my formatter is:

'formatters': {
                'logfileFormat': {
                    'format': '%(asctime)s %(name)-12s: %(levelname)s %(message)s'
                }
            }

When there's an exception, I would like the formatter to show the error message, as well as print out the exec_info. How do I alter the format to do this? Or do I need to implement my own formatter in code, and reference it here?

2 Answers

I may be missing something here, but calling logger.exception(e) already adds the stack trace and error message.

(While logger.error(e) only shows the message)

See this POC script:

import logging
import logging.config

logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'logfileFormat': {
            'format': '%(asctime)s %(name)-12s: %(levelname)s %(message)s'
            }
        },
    'handlers': {
        'logfile': {
            'level': 'INFO',
            'formatter': 'logfileFormat',
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',  # Default is stderr
            },
        },
    'loggers': {
        '': {  # root logger
            'handlers': ['logfile'],
            'level': 'INFO',
            'propagate': False
            },
        }
    })

def foo():
    logging.info("Normal stuff")
    logging.warning("More stuff")

    try:
        # ... something causing an exception ...
        raise Exception("Something something, Exception")
    except Exception as e:
        logging.exception(e)

    try:
        raise Exception("The stacktrace of this is surpressed")
    except Exception as e:
        logging.error(e)

    logging.info("More normal stuff")

foo()

Which results in the following output:

2020-07-26 20:54:27,621 root        : INFO Normal stuff
2020-07-26 20:54:27,621 root        : WARNING More stuff
2020-07-26 20:54:27,621 root        : ERROR Something something, Exception
Traceback (most recent call last):
  File "sol.py", line 34, in foo
    raise Exception("Something something, Exception")
Exception: Something something, Exception
2020-07-26 20:54:27,622 root        : ERROR The stacktrace of this is surpressed
2020-07-26 20:54:27,622 root        : INFO More normal stuff

I fail to see in what way your existing formatter needs to be changed.

If you wanted your stack trace to be indented or otherwise formatted differently, see this SO answer for creating a custom formatter.

logging automatically adds the traceback to your message if you use log.exception(e).

If you want to log the traceback with other logging levels, you can use the exc_info=True kwarg like log.error("message", exc_info=True).

You don't need to modify the formatter for the traceback to show up.

Related