python-json-logger icon indicating copy to clipboard operation
python-json-logger copied to clipboard

how to log exception with extra?

Open memelet opened this issue 6 years ago • 1 comments

Is this possible?

    except IOError as io_msg:
        log.error(f'Failed to open device data', extra={"device": log_prefix}, io_msg)

memelet avatar Oct 06 '19 15:10 memelet

What are you trying to achieve?

If you want to provide extra data alongside the exception data, you can use log.exception(message, extras) or log.error(message, extras, exc_info=True). exc_info is available on all logging methods and tells the logging system to collect and format... exception info.

logging.exception is a convenience alias for logging.error(..., exc_info=True).

jsonlogger adds the formatted exception information as the exc_info on the generated record.

If you just want to store the exception message as an extra, add the stringification of the exception object (which is what you're actually catching) to the dict the normal way: log.error(msg, extra={..., 'error_message': str(io_msg)}).

xmo-odoo avatar Nov 19 '19 10:11 xmo-odoo