Frage

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).

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top