在Python中将*args传递给string.format?
是否可以将*args传递给string.format?我有以下功能:在Python中将*args传递给string.format?,python,logging,args,Python,Logging,Args,是否可以将*args传递给string.format?我有以下功能: @classmethod def info(cls, component, msg, *args): """Log an info message""" cls.__log(cls.Level.INFO, component, msg, args) @classmethod def __log(cls, level, component, msg, *args): """Log a message a
@classmethod
def info(cls, component, msg, *args):
"""Log an info message"""
cls.__log(cls.Level.INFO, component, msg, args)
@classmethod
def __log(cls, level, component, msg, *args):
"""Log a message at the requested level"""
logging.getLogger("local").log(level, " - ".join([component, msg.format(args)]))
当我尝试使用LogCapture进行单元测试时,我得到以下结果:
def test_logWithArgs(self):
Logger.level(Logger.Level.INFO)
with LogCapture(level=Logger.Level.INFO) as lc:
Logger.info("MyComponent", "{0}", "TestArg")
lc.check(("local", "INFO", "MyComponent - TestArg"))
AssertionError: Sequence not as expected:
same:
()
first:
(('local', 'INFO', 'MyComponent - TestArg'),)
second:
(('local', 'INFO', "MyComponent - (('TestArg',),)"),)
我想你想做的是
msg.format(*args)
是的
>>> a=(1,2,3,4)
>>> "{0}{1}{2}{3}".format(*a)
'1234'
这将导致以下
(('local','INFO','MyComponent-('TestArg',)”),
您也必须更改它INFO:cls.\u log(cls.Level.INFO,component,msg,*args)
我想您已经得到了答案-但我应该让您参考logging.Formatter工具