Logging formatter has a lowercase name

I have a simple registrar here. I do not want the level name to be complete, but lowercase. How can i fix this?

import logging

log = logging.getLogger(__name__)

ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
chformatter = logging.Formatter('%(levelname)s %(message)s')
ch.setFormatter(chformatter)

log.addHandler(ch)

log.error("blah")
+4
source share
2 answers

Browse through the documentation for the logging module , and you will find a function that allows you to set arbitrary names for logging levels. Use this.

>>> log.error("blah")
ERROR blah
>>> logging.addLevelName(logging.ERROR, 'error')
>>> log.error("blah")
error blah
+6
source

You can create your own formatter:

class MyFormatter(logging.Formatter):
    def format(self, record):
        record.levelname = record.levelname.lower()
        return logging.Formatter.format(self, record)

chformatter = MyFormatter('%(levelname)s %(message)s')
ch.setFormatter(chformatter)
0
source

Source: https://habr.com/ru/post/1531067/


All Articles