Python日志记录模块抛出TypeError

时间:2017-02-07 22:29:54

标签: python python-2.7

我遇到了Python'logging'库的问题,引发了以下错误:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit
    msg = self.format(record)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format
    return fmt.format(record)
  File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 469, in format
    s = self._fmt % record.__dict__
TypeError: not enough arguments for format string
Logged from file FILE_NAME.py, line 98

在调试器中单步执行记录。 dict 具有我的格式所需的所有参数(见下文)。

当我改变执行库的引导逻辑的方式时,就开始发生这种情况了。我将boostrapping逻辑移到了一个类(见下文),现在从我的应用程序的开始调用它。

class SetupLogging:
    # Use class variable as 'singleton' to track if complete or not
    _logging_bootstrap_complete = False

    def bootstrap_logging(self):
        if not SetupLogging._logging_bootstrap_complete:
            logging.basicConfig(format='%(asctime)s [%s(filename)s:%(lineno)d] %(levelname)s: %(message)s',
                            level=logging.INFO, filename='FILE.log')
            SetupLogging._logging_bootstrap_complete = True

调用日志记录引导程序:

import Utilities

logger_boot = Utilities.SetupLogging()
logger_boot.bootstrap_logging()

对于我的生活,我无法弄清楚为什么这种变化会以这种方式影响模块,我们将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的格式字符串中有一个额外的valueHigh,例如:

s

摆脱named placeholder之前的>>> '%(asctime)s %s(filename)s' % {'asctime': '201602072243', ... 'filename': 'output.log'} Traceback (most recent call last): TypeError: not enough arguments for format string

s