Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将*args传递给string.format?_Python_Logging_Args - Fatal编程技术网

在Python中将*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

是否可以将*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 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工具