Python logging.Formatter(): is there any way to fix the width of a field and justify it left/right?

Field width can be specified by adding a number in front of the type specifier:

>>> "%(foo)8s" % {'foo': 'bar'}
'     bar'

You can use this in your format string passed to the formatter. For your example, that’d be:

"%(asctime)s %(filename)s: %(levelname)8s %(message)s"

Leave a Comment