Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the level name be lower-case?

Tags:

python

logging

I have a simple logger here. I don't want the levelname to be all caps but lower-case (e.g., info instead of the default INFO). How can I fix it?

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")
like image 666
Kiarash Avatar asked Mar 10 '14 23:03

Kiarash


2 Answers

Look into the documentation for the logging module and you will find a function that lets you set arbitrary names to logging levels. Go use that.

>>> log.error("blah")
ERROR blah
>>> logging.addLevelName(logging.ERROR, 'error')
>>> log.error("blah")
error blah
like image 159
metatoaster Avatar answered Nov 18 '22 04:11

metatoaster


You can make 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)
like image 45
n611x007 Avatar answered Nov 18 '22 05:11

n611x007