简体   繁体   中英

Python Logging Module Throwing TypeError

I am having issues with the Python 'logging' library throwing the following error:

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

Stepping through in the debugger the record. dict has all the required parameters for my format (see below).

This started occuring when I changed how my bootstrapping logic for the library is executed. I moved the boostrapping logic to a class (see below) and now call it from my application's start.

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

Call logging bootstrap:

import Utilities

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

For the life of me I cannot figure out why this change would affect the module in this way and any help would be greatly appreciated.

You have an extra s in your format string, eg:

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

Get rid of the s before the named placeholder :

>>>  '%(asctime)s: %(filename)s' % {'asctime': '201602072243',
...                                'filename': 'output.log'}
'201602072243: output.log'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM