我正在尝试创建一个自定义记录器,在那里我想在stdout上打印所有控制台级别的消息。对于剩余部分,我不想在日志中给出任何处理程序。
所以我创建了一个控制台级别,
CONSOLE = 60 def console(msg, *args, **kwargs): if logging.getLogger().isEnabledFor(CONSOLE): logging.log(CONSOLE, msg, args, **kwargs)
logging.Logger.console = console
为了获得一个只在stdout上打印控制台级别日志的记录器,我使用了下面的代码
`logging.basicConfig(format="%(asctime(s%(name(s%;,level=日志记录。INFO,datefmt="%Y-%m-%d%H:%m:%S"(
streamhandler = logging.StreamHandler()
streamhandler.setLevel(CONSOLE)
logging.getLogger('').addHandler(streamhandler)
logger = logging.getLogger("ABC")
logging.Logger.console = console`
但我的错误是-
22:46:55.280 ERROR Failed to log following message properly: This is my message 22:46:55.280 DEBUG TypeError: not all arguments converted during string formatting Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 369, in getMessage msg = msg % self.args
我不得不在控制台方法-中将args更新为*args
def console(msg, *args, **kwargs):
if logging.getLogger().isEnabledFor(CONSOLE):
logging.log(CONSOLE, msg, *args, **kwargs)