Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a python logging is there a formatter to truncate the string?

Python logging formats strings with a syntax I don't see elsewhere in python, like

 'format': '%(name)s'

Is there any way to truncate an error message using the formatter, or do I need to override the LogRecord class for that?

This truncates parts of the message (though I can't find the documentation for this feature in the normal places):

 'format': '%(name).40s %(message).40s'

I'd rather truncate the entire formatted message, if possible (for an 80 column console, say).

like image 431
hobs Avatar asked May 28 '13 18:05

hobs


People also ask

How is logging done in Python?

Python has a built-in module logging which allows writing status messages to a file or any other output streams. The file can contain the information on which part of the code is executed and what problems have been arisen.

What is logging DEBUG in Python?

Debugging is an important step of any software development project. The logging module is part of the standard Python library, provides tracking for events that occur while software runs, and can output these events to a separate log file to allow you to keep track of what occurs while your code runs.

What is Python logging propagate?

Python Logger 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.

Is Python logging process safe?

Although logging module is thread-safe, it's not process-safe. If you want multiple processes to write to the same log file, then you have to manually take care of the access to your file.


1 Answers

This is just the old style of string formatting. I think you can just use a ".L", where L is the length to truncate the string to whatever length you like. eg:

'format': '%(name).5s'  

would truncate the length to 5 characters.

It's a little hard to find, but they actually do mention it in the docs:

The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content

like image 98
Gerrat Avatar answered Sep 18 '22 11:09

Gerrat