You can use the precision field to set the maximum field width:
formatter = logging.Formatter('%(levelname).1s %(message)s')
.1
sets the field width to no more than one character, trimming 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 documentation for line formatting operations :
Conversion: 's'
Value: String (converts any Python object using str()
).
Notes: (6)
- [...] Accuracy determines the maximum number of characters used.
source share