Change levelname format in logRecord

You can use the precision field to set a maximum field width:

formatter = logging.Formatter('%(levelname).1s %(message)s')

.1 sets the field width to at most one character, truncating the level to the first character:

>>> for level in ('CRITICAL', 'ERROR', 'INFO', 'WARNING', 'DEBUG'):
...     print '%(level)-.1s %(message)s' % {'level': level, 'message': 'Hello world!'}
... 
C Hello world!
E Hello world!
I Hello world!
W Hello world!
D Hello world!

See the String Formatting Operations documentation:

Conversion: 's'
Meaning: String (converts any Python object using str()).
Notes: (6)

  1. [...] The precision determines the maximal number of characters used.

If you want completely different levelname then use logging.addLevelName

logging.addLevelName(logging.DEBUG, 'DETAILED')
logging.addLevelName(logging.INFO, 'GENERAL')

Tags:

Python

Logging