为什么要log.warning("%s",s)?

时间:2015-03-10 09:48:08

标签: python logging

有人可以从/usr/lib/python2.7/logging/__init__.py解释此方法的标记行吗?

def _showwarning(message, category, filename, lineno, file=None, line=None):
    """
    Implementation of showwarnings which redirects to logging, which will first
    check to see if the file parameter is None. If a file is specified, it will
    delegate to the original warnings implementation of showwarning. Otherwise,
    it will call warnings.formatwarning and will log the resulting string to a
    warnings logger named "py.warnings" with level logging.WARNING.
    """
    if file is not None:
        if _warnings_showwarning is not None:
            _warnings_showwarning(message, category, filename, lineno, file, line)
    else:
        s = warnings.formatwarning(message, category, filename, lineno, line)
        logger = getLogger("py.warnings")
        if not logger.handlers:
            logger.addHandler(NullHandler())
        logger.warning("%s", s)  # <------ I don't understand this line

为什么最后一行不是这个:

logger.warning(s)

1 个答案:

答案 0 :(得分:-2)

因为这是正确的语法。 见https://docs.python.org/2/library/logging.html

Logger.warning(msg, *args, **kwargs)

Logs a message with level WARNING on this logger. The arguments are interpreted as for debug().

这种语法是众所周知的,例如所有* printf()函数都使用它。