我正在 python 中创建新的日志级别,但出现错误 "Failed to log following message properly:"
I am creating a new log level in python, but I am getting error "Failed to log following message properly:"
我正在尝试创建一个自定义记录器,我想在 stdout 上打印所有控制台级别的消息。对于剩下的部分,我不想在日志记录中提供任何处理程序。
所以我创建了一个控制台关卡,
CONSOLE = 60 def console(msg, *args, **kwargs): if logging.getLogger().isEnabledFor(CONSOLE): logging.log(CONSOLE, msg, args, **kwargs)
logging.Logger.console = console
为了获得一个只在标准输出上打印控制台级别日志的记录器,我使用了类似下面的代码-
`
logging.basicConfig(format="%(asctime)s %(name)s %(levelname)s %(message)s",level=logging.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)
我正在尝试创建一个自定义记录器,我想在 stdout 上打印所有控制台级别的消息。对于剩下的部分,我不想在日志记录中提供任何处理程序。
所以我创建了一个控制台关卡,
CONSOLE = 60 def console(msg, *args, **kwargs): if logging.getLogger().isEnabledFor(CONSOLE): logging.log(CONSOLE, msg, args, **kwargs)
logging.Logger.console = console
为了获得一个只在标准输出上打印控制台级别日志的记录器,我使用了类似下面的代码-
` logging.basicConfig(format="%(asctime)s %(name)s %(levelname)s %(message)s",level=logging.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)