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")
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With