首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中重写logging.Formatter

在Python中重写logging.Formatter
EN

Stack Overflow用户
提问于 2012-02-21 21:54:39
回答 1查看 3.1K关注 0票数 4

我创建了一个覆盖格式化程序,它像这样屏蔽了一些字符串:

代码语言:javascript
运行
复制
class MaskFormatter(logging.Formatter):

    def __init__(self, fmt, mask):
        logging.Formatter.__init__(self, fmt, mask)
        self.mask = mask

    def format(self, record):
        result = logging.Formatter.format(self, record)
        if result is not None and result.find(self.mask) != -1:
            result = result.replace(self.mask, '*' * len(self.mask))
        return result    

我是这样使用它的:

代码语言:javascript
运行
复制
formatter = MaskFormatter('%(asctime)s %(levelname)s' + 
    ' %(module)s %(lineno)d %(message)s', mask='abcde')
hdlr.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
hdlr.setFormatter(formatter)

但是,在我的日志行中,我注意到datetime现在也被屏蔽了!!虽然我要求它只遮盖“abcde的”有人可以帮助吗?

所有日志行的前缀都是*****,而不是real datetime:

代码语言:javascript
运行
复制
***** DEBUG mail
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-21 22:50:07

查看日志格式化程序的文档,发现格式化程序有两个可选参数。第一个是消息格式,第二个是日期时间格式。看起来您正在以datetime格式发送掩码。尝试从logging.Formatter.init调用中删除掩码参数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9378772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档