I am using python’s log formatter to format log records and i have a fmt value of
fmt = "[%(filename)s:%(lineno)s] %(message)s"
What i would like is that “[file.py:20]” to be stretched to 10 characters wide (for example). If it was one value that would have been easy but is there any way to stretch this entire structure to a specified length?
I want something like:
tmp = "[%(filename)s:%(lineno)s]"
fmt = "%(tmp)10s %(message)s"
I would like to know if this is possible using string formatting or if I can trick python’s formatter somehow to get what i want..
Option 1
Start here: http://docs.python.org/library/logging.html#formatter-objects
You’ll create your own customized subclass of
Formatterthat provides it’s own uniqueformatmethod.Then you must be sure to call
setFormatter()in each of yourHandlersso that they use your new formatter.Option 2
Create your own subclass of LogRecord with the additional property.
Subclass
Loggerand overridemakeRecordto create your new subclass ofLogRecord.Provide a customized format that uses this new property value.