Custom logging code(s) / numbers to the log level Error

Peter Otten __peter__ at web.de
Fri Feb 22 17:00:49 EST 2019


kibtes2 at gmail.com wrote:

> I wanted to write/extend the logging library to have a custom Error number
> for each exception (error) the code has. What's the best approach for
> this? If possible can someone please provide a good starting code snippet.
> Thanks

You can pass arbitrary data via the `extra` parameter:

$ cat log_errno.py
import logging

logger = logging.getLogger()
logging.basicConfig(format="ERROR #%(errno)s: %(message)s")

logger.error("First", extra=dict(errno=42))
logger.error("Second", extra=dict(errno=123))
$ python3 log_errno.py 
ERROR #42: First
ERROR #123: Second

If that's not you want you have to invest more time into a better 
description of your goal, with example code and output.



More information about the Python-list mailing list