Kiarash
Kiarash

Reputation: 8018

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

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")

Upvotes: 7

Views: 4969

Answers (2)

n611x007
n611x007

Reputation: 9272

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)

Upvotes: 7

metatoaster
metatoaster

Reputation: 18928

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

Upvotes: 12

Related Questions