2024-10-02 18:14:53 发布
网友
我想知道如何使用python的日志包更改logrecod中levelname的格式。在
formatter = logging.Formatter('%(levelname)-8s %(message)s')
基本上,我想将任何日志名称替换为名称的第一个字母。例如
等等
您可以使用精度字段来设置最大字段宽度:
formatter = logging.Formatter('%(levelname).1s %(message)s')
.1将字段宽度设置为最多一个字符,将级别截断为第一个字符:
.1
参见String Formatting Operations documentation:
Conversion: 's'Meaning: String (converts any Python object using str()).Notes: (6)[...] The precision determines the maximal number of characters used.
Conversion: 's'Meaning: String (converts any Python object using str()).Notes: (6)
's'
str()
如果您想要完全不同的levelname,那么使用日志记录.addLevelName在
logging.addLevelName(logging.DEBUG, 'DETAILED') logging.addLevelName(logging.INFO, 'GENERAL')
您可以使用精度字段来设置最大字段宽度:
^{pr2}$.1
将字段宽度设置为最多一个字符,将级别截断为第一个字符:参见String Formatting Operations documentation:
如果您想要完全不同的levelname,那么使用日志记录.addLevelName在
相关问题 更多 >
编程相关推荐