0%

如何在Python logging.Formatter中字段右对齐

❓ 我目前正在尝试在Python记录器中右对齐记录级别字段,现在输出如下:

1
2
3
4
[2011-10-14 13:47:51] [DEBUG] --- starting... (smtphandlers.py:96)
[2011-10-14 13:47:51] [INFO] --- first things first... (smtphandlers.py:97)
[2011-10-14 13:47:51] [WARNING] --- about to end... (smtphandlers.py:98)
[2011-10-14 13:47:51] [DEBUG] --- ending (smtphandlers.py:99)

我希望这样:

1
2
3
4
[2011-10-14 13:47:51] [   DEBUG] --- starting... (smtphandlers.py:96)
[2011-10-14 13:47:51] [ INFO] --- first things first... (smtphandlers.py:97)
[2011-10-14 13:47:51] [ WARNING] --- about to end... (smtphandlers.py:98)
[2011-10-14 13:47:51] [ DEBUG] --- ending (smtphandlers.py:99)

第一个块的格式行是:

1
logging.Formatter("[%(asctime)s] [%(levelname)s] --- %(message)s (%(filename)s:%(lineno)s)", "%Y-%m-%d %H:%M:%S")

✔️

1
logging.Formatter("[%(asctime)s] [%(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s)", "%Y-%m-%d %H:%M:%S")