Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging.Formatter(): is there any way to fix the width of a field and justify it left/right?

Here's a sample of log records from the logging tutorial:

2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message 2005-03-19 15:38:55,979 - simpleExample - INFO - info message 2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message 2005-03-19 15:38:56,055 - simpleExample - ERROR - error message 2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message 

This trailing jaggedness annoys me to no end.

I really want to be able to format like this:

2005-03-19 15:38:55,977 - simpleExample -    DEBUG - debug message 2005-03-19 15:38:55,979 - simpleExample -     INFO - info message 2005-03-19 15:38:56,054 - simpleExample -  WARNING - warn message 2005-03-19 15:38:56,055 - simpleExample -    ERROR - error message 2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message 

I've attempted the following for my logger, which doesn't work (abbreviated code):

fmt = "{0:>8}" formatter = logging.Formatter("%(asctime)s %(filename)s: " + fmt.format("%(levelname)s") + " %(message)s", "%Y/%m/%d %H:%M:%S") 

This executes fine and prints the level name as always, but it doesn't implement the width format.

Ex.

logger.debug("testing debug message") logger.info("some random info") logger.critical("oh crap!") 

Actual result:

2013/12/16 13:43:10 logtester: DEBUG testing debug message 2013/12/16 13:43:10 logtester: INFO some random info 2013/12/16 13:43:10 logtester: CRITICAL oh crap! 

Desired result:

2013/12/16 13:43:10 logtester:    DEBUG testing debug message 2013/12/16 13:43:10 logtester:     INFO some random info 2013/12/16 13:43:10 logtester: CRITICAL oh crap! 

Any hints to implement a fixed width of a field in a logging.Formatter()?

Using Python 2.6.9.

EDIT: second attempt using another method:

formatter = logging.Formatter("%(asctime)s %(filename)s: " + "%(foo)5s" % {"foo" : "(%(levelname)s)"} + " %(message)s", "%Y/%m/%d %H:%M:%S") 

Still results in:

2013/12/16 13:43:10 logtester: DEBUG testing debug message 2013/12/16 13:43:10 logtester: INFO some random info 2013/12/16 13:43:10 logtester: CRITICAL oh crap! 

Maybe I'm just doing something boneheaded.

like image 771
CptSupermrkt Avatar asked Dec 16 '13 18:12

CptSupermrkt


People also ask

How do you print a fixed width string?

Bookmark this question. Show activity on this post. def splitter(str): for i in range(1, len(str)): start = str[0:i] end = str[i:] yield (start, end) for split in splitter(end): result = [start] result.

What is propagate in python logging?

Propagate: Decides whether a log should be propagated to the logger's parent. By default, its value is True. A level: Like the log handler level, the logger level is used to filter out “less important” logs.


1 Answers

Field width can be specified by adding a number in front of the type specifier:

>>> "%(foo)8s" % {'foo': 'bar'} '     bar' 

You can use this in your format string passed to the formatter. For your example, that'd be:

"%(asctime)s %(filename)s: %(levelname)8s %(message)s" 
like image 55
Amber Avatar answered Sep 21 '22 09:09

Amber