Python Forum
How to left align logging messages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to left align logging messages
#1
Hi,
I use the below code, and I want the messages to be left-aligned, meaning I want to align the columns.

import logging
from datetime import datetime

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s.%(msecs)04d:%(levelname)s: %(message)s',
                    filename=('mylog'+datetime.today().strftime('%Y%m%d_%H%M%S')+".txt"),
                    filemode='w',datefmt='%Y-%m-%d %H:%M:%S')
logging.debug('A debug message')
logging.info('Info message')
logging.warning('Warning message')
current output:
2020-06-27 11:52:26.201:DEBUG: A debug message
2020-06-27 11:52:26.202:INFO: Info message
2020-06-27 11:52:26.202:WARNING: Warning message
desired output:

2020-06-27 11:52:26.201:DEBUG:   A debug message
2020-06-27 11:52:26.202:INFO:    Info message
2020-06-27 11:52:26.202:WARNING: Warning message
Reply
#2
The maximum you would need would be 8 spaces for CRITICAL. So tell it you want to pad to 8 spaces and use a hyphen for left alignment.

Change
    format='%(asctime)s.%(msecs)04d:%(levelname)s: %(message)s',
to

    format='%(asctime)s.%(msecs)04d:%(levelname)-8s: %(message)s',
Output:
2020-06-26 22:42:13.0384:DEBUG : A debug message 2020-06-26 22:42:13.0384:INFO : Info message 2020-06-26 22:42:13.0384:WARNING : Warning message 2020-06-26 22:42:13.0385:CRITICAL: Critical
(or -7 if you want WARNING to be the max and would put up with a bump if a CRITICAL ever appeared).
Reply
#3
How can we decide -8s or -7s or any other like -xs
Reply
#4
For logging (where you don't want to delay the messages), you just need to decide ahead of time what you want. You know that CRITICAL is the longest possible message there, so 8 makes sense.

For normal output, you might want to examine all of the strings and then figure out which is the longest. But you have to wait for all the output to be generated to make that decision. That doesn't make sense for logging where you want the information to be available quickly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can I use logging in a class (without multiple messages) mevan 2 535 Oct-16-2023, 11:08 PM
Last Post: mevan
  How to auto align x-axis label SamLiu 2 845 Jan-27-2023, 11:10 PM
Last Post: SamLiu
  PyQT5 - align left frohr 7 3,836 May-07-2022, 09:56 PM
Last Post: deanhystad
  How did one column get left-justified? Mark17 6 1,880 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  Explanation of the left side of this statement please rascalsailor 3 2,451 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Center align Kristenl2784 1 1,920 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 3,908 Dec-28-2019, 04:04 PM
Last Post: SriRajesh
  Why is left mouse click not working? yeto 3 6,075 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  logging messages ahead of print messages vindo 6 3,140 Jun-18-2019, 02:45 PM
Last Post: vindo
  str.format rounding to the left of the decimal ClassicalSoul 2 2,447 Mar-27-2019, 11:12 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020