python - 在 python 日志记录中是否有一个格式化程序来截断字符串?

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/

相关文章:

Python - 为字符串格式化运算符解包列表的简短方法?

python - .format() 使用 {0 :g} to remove trailing ze

input - AngularJs:如何在输入中格式化数据?

android - PhoneNumberFormattingTextWatcher 不工作

python - python - 如何使用单行将列表/元组转换为python中的空格分隔字符串?

html - 如何使用 CSS 自动添加节号(1.2、3.4.1)?

c# - DateTime.ToString 格式

python - 使用其 API 和 Python 在 Google 表格电子表格中设置单元格格式

java - 更改 JTable 单元格颜色

python - 带有 float 的格式字符串中的千位分隔符