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
  Explanation of the left side of this statement please rascalsailor 3 1,149 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Center align Kristenl2784 1 968 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 2,002 Dec-28-2019, 04:04 PM
Last Post: SriRajesh
  Why is left mouse click not working? yeto 3 3,395 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  logging messages ahead of print messages vindo 6 1,615 Jun-18-2019, 02:45 PM
Last Post: vindo
  str.format rounding to the left of the decimal ClassicalSoul 2 1,384 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Move a character left or right in a file. DreamingInsanity 4 2,625 Mar-21-2019, 07:52 PM
Last Post: DreamingInsanity
  sometimes it fails with no messages Skaperen 2 1,356 Jul-23-2018, 05:59 AM
Last Post: Skaperen
  Can't change Right to left id spawning/ movement to top to bottom Kingrocket10 3 2,456 Dec-14-2017, 11:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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