I’m using python logging and I have a formatter that looks like the following:
formatter = logging.Formatter(
'%(asctime)s - %(pathname)86s - %(lineno)4s - %(message)s', '%d %H:%M'
)
As you can see, I like the information in my log files to line up neatly in columns. The reason I have 86 spaces reserved for the pathname is because the full paths to some of the files used in my program are that long. However, all I really need is the actual file name, not the full path. How can I get the logging module to give me just the file name? Better yet, since I have some long filenames, I’d like the first 3 characters of the filename, followed by ‘~’, then the last 16 characters. So
/Users/Jon/important_dir/dev/my_project/latest/testing-tools/test_read_only_scenarios_happily.py
should become
tes~arios_happily.py
You’ll have to implement your own Formatter subclass that truncates the path for you; the formatting string cannot do this.
Since you want to use just the filename you’d want to use the
filenameparameter as the base, which we can then truncate as needed.For the size-limited version, I’d actually use a new record attribute for, leaving the existing
filenameto be the full name. Lets call itfilename20.Use this class instead of the normal
logging.Formatterinstance, and reference thefilename20field in the formatting string:Note that I also shortened the space needed for the field to 20 characters:
%(...)20s.If you have to specify the formatter in a ConfigParser-style configuration file, create a named formatter via a
[formatter_NAME]section, and give that section aclasskey:and then you can use
formatter=truncatedin any handler sections you define.For the dictionary-schema style configuration, the same applies: specify the formatter class via the
classkey in any formatter definition.