“Python日志中有截断字符串的格式化工具吗?”

2024-05-29 04:49:18 发布

您现在位置:Python中文网/ 问答频道 /正文

Python logging用python中其他地方看不到的语法格式化字符串,比如

 'format': '%(name)s'

是否有任何方法可以使用格式化程序截断错误消息,或者我需要为此重写LogRecord类?

这会截断消息的部分内容(尽管我在normal places中找不到此功能的文档):

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

如果可能的话,我宁愿截断整个格式化的消息(比如80列控制台)。


Tags: 方法字符串name程序功能format消息内容
1条回答
网友
1楼 · 发布于 2024-05-29 04:49:18

这只是旧的字符串格式。 我想你可以用一个“.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

相关问题 更多 >

    热门问题