简体   繁体   中英

Python Logging module formatter

Having a hard time understanding the formatter options. specifically the string replacements. If I have a really long python file name, how do I get it to cut it off to keep everything even?

formatter = logging.Formatter('%(asctime)s %(name)-15s %(threadName)-10s %(levelname)-8s %(message)s')

while I understand that I can make the -15s part longer to give me more space between, how do I get it to cut off a long filename say 'this_is_a_test_ok.py' rather than just padding everything out? I want my final output to be very columnar.

Just tested it:

formatter = logging.Formatter('%(asctime)s %(name)-15.5s %(threadName)-10s %(levelname)-8s %(message)s')

Truncates the loggers's name to 5 chars. How to combine that with padding is another question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM