Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging python change log level text

Tags:

python

logging

with the library

import logging

when I use the method .error(text) or .warning(text), the logger writes the log level as INFO, WARNING, ERROR, etc.

I was wondering if there is a way to change the WARNING string, for example, to WARN (and change ERROR as ERR, instead).

TLDR; I want to change the Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') in whatever I like... Is there a way to do that?

like image 419
salvob Avatar asked Oct 25 '25 04:10

salvob


1 Answers

That comes out of the box in the logging module, with the addLevelName function:

logging.addLevelName(logging.WARNING, "WARN")
logging.addLevelName(logging.ERROR, "ERR")

Those name will be used by all the formatters.

like image 168
Serge Ballesta Avatar answered Oct 26 '25 17:10

Serge Ballesta