Python记录器,打印没有父名称的子记录器%(name) [英] Python logger, print child logger %(name) without parent name

查看:80
本文介绍了Python记录器,打印没有父名称的子记录器%(name)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python记录日志.

I am trying Python logging.

由于我希望某些记录器共享一个格式化程序,因此我使用如下所示的日志记录层次结构.

Since I want some logger to share a formatter, I use hierarchy of logging like the below code.

如您所见,格式为'%(asctime)s - %(name)s - %(message)s'

这时,我希望记录器打印不带父名称的%(name)s部分.

At this point, I want loggers to print the %(name)s part without the parent name.

我的意思是,通常它将名称打印为'SYS.Start Up Routine',但我想要'Start Up Routine'

I mean, normally it will print the name as 'SYS.Start Up Routine' but I want 'Start Up Routine'

有什么办法吗?

logging.root.level = logging.DEBUG
logger = logging.getLogger('SYS')
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')

streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

logger1 = logging.getLogger("SYS.Start Up Routine")
logger2 = logging.getLogger("SYS.Message Layer")
logger3 = logging.getLogger("SYS.Application Layer")

推荐答案

如果您想要名称说"bar"而不是"foo.bar",只需登录到名为"bar"的记录器,然后将处理程序添加到根记录器.

If you want the name to say 'bar' instead of 'foo.bar', just log to a logger named 'bar', and add your handlers to the root logger.

这篇关于Python记录器,打印没有父名称的子记录器%(name)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆