Python logging用我在 python 其他地方看不到的语法格式化字符串,比如
'format': '%(name)s'
有什么方法可以使用格式化程序截断错误消息,还是我需要为此重写 LogRecord 类?
这会截断部分消息(尽管我在 normal places 中找不到此功能的文档):
'format': '%(name).40s %(message).40s'
如果可能的话,我宁愿截断整个格式化消息(例如,对于 80 列控制台)。
最佳答案
这只是字符串格式的旧样式。 我认为您可以只使用“.L”,其中 L 是将字符串截断为您喜欢的任何长度的长度。例如:
'format': '%(name).5s'
会将长度截断为 5 个字符。
有点难找,但他们确实在 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
https://stackoverflow.com/questions/16799075/