我使用LoggerAdapter
让python日志输出Linux tid,而不是长的惟一id。但这样我不会修改现有的logger
,而是创建一个新对象:
new_logger = logging.LoggerAdapter(
logger=logging.getLogger('mylogger'),
extra=my_tid_extractor())
现在我希望这个LoggerAdapter
被某些模块使用。只要我知道全局变量用作记录器,我就可以这样做:
somemodule.logger = new_logger
但这并不好-它只在几种情况下工作,您需要知道模块使用的记录器变量。
你知道一种使LoggerAdapter
在全球可用的方法吗,例如,通过调用s.th。就像
logging.setLogger('mylogger', new_logger)
或者:是否有其他方法让Python logging
输出Linux线程id,比如由ps
打印?
我认为您需要override LoggerAdapter.process()方法 因为默认的LoggerAdapter.process方法将不起作用,下面是示例:
或者,您可以实现自定义记录器,并在日志模块中将其设为默认值。
以下是示例:
输出样本:
相关问题 更多 >
编程相关推荐