Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align logging messages of different levels

I'm using Python's logging module like this:

class InitLogging():
    def LogModule(self):
        self.Logger          = logging.getLogger("_TCP_Logger")
        self.Logger.LogLevel = logging.DEBUG
        self.Logger.setLevel(self.Logger.LogLevel)
        self.fh              = logging.FileHandler("Log.txt")
        self.fh.level        = logging.DEBUG
        self.formatter       = logging.Formatter('%(asctime)s %(levelname)s %(message)8s')
        self.Logger.addHandler(self.fh)        
        self.Logger.log(logging.INFO,"=====================================")
        self.Logger.log(logging.INFO,"Start Logging Module!!")
        self.Logger.log(logging.INFO,"=====================================")
        self.fh .setFormatter(self.formatter)
        self.Logger.log(logging.INFO,"Enrty to Logging Module!!")

class LogMsg(InitLogging):
    def Logit(self):
        self.LogModule()
        self.Logger.log(logging.DEBUG,'This is Debugging Message!!')
        self.Logger.log(logging.INFO,"This is Info Message!!")
        self.Logger.log(logging.WARNING,"This is WARNING Message!!")

LoggingInstance = LogMsg()
LoggingInstance.Logit()

When I run my code I get the output below in Log.txt:

=====================================
Start Logging Module!!
=====================================

2013-03-20 17:51:35,135 INFO Enrty to Logging Module!!
2013-03-20 17:51:35,135 DEBUG This is Debugging Message!!
2013-03-20 17:51:35,135 INFO This is Info Message!
2013-03-20 17:51:35,135 WARNING This is WARNING Message!!

But I'd like to align messages of different log levels together, producing output like below:

=====================================
Start Logging Module!!
=====================================
2013-03-20 17:51:35,135 INFO       Enrty to Logging Module!!
2013-03-20 17:51:35,135 DEBUG      This is Debugging Message!!
2013-03-20 17:51:35,135 INFO       This is Info Message!
2013-03-20 17:51:35,135 WARNING    This is WARNING Message!!

How can I do this?

like image 204
chandan kumar Avatar asked Mar 20 '13 12:03

chandan kumar


People also ask

What are the five levels of logging in Python?

In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.

How many levels are defined in logging module?

There are six log levels in Python; each level is associated with an integer that indicates the log severity: NOTSET=0, DEBUG=10, INFO=20, WARN=30, ERROR=40, and CRITICAL=50.

What are logging messages?

The message logging facility, when active, writes messages to the log data set containing all data that simulated resources transmit or receive in a specified network.

What is logging level in Python?

Logging Levels When you set a logging level in Python using the standard module, you're telling the library you want to handle all events from that level on up. If you set the log level to INFO, it will include INFO, WARNING, ERROR, and CRITICAL messages. NOTSET and DEBUG messages will not be included here. Level.


1 Answers

Add a width to the levelname field in the formatter's format string.

This is left-aligned:

self.formatter = logging.Formatter('%(asctime)s %(levelname)-10s %(message)s')

                         levelname
                       |----------|
2013-03-20 16:46:50,573 INFO       Enrty to Logging Module!!
2013-03-20 16:46:50,573 DEBUG      This is Debugging Message!!
2013-03-20 16:46:50,573 INFO       This is Info Message!!
2013-03-20 16:46:50,573 WARNING    This is WARNING Message!!

This is right-aligned:

self.formatter = logging.Formatter('%(asctime)s %(levelname)10s %(message)s')

                         levelname
                       |----------|
2013-03-20 16:51:04,648       INFO Enrty to Logging Module!!
2013-03-20 16:51:04,650      DEBUG This is Debugging Message!!
2013-03-20 16:51:04,650       INFO This is Info Message!!
2013-03-20 16:51:04,650    WARNING This is WARNING Message!!
                       |0123456789|
                       | width=10 |

String formatting documentation

like image 128
Pavel Anossov Avatar answered Oct 17 '22 20:10

Pavel Anossov