使用python中的logger模块return self._fmt % values TypeError: not enough arguments for format string错误解决办法
由上图,可以看出应该是format设置格式化器的出现了错误,应该是格式化样式出现了错误。logger.info("这个是info级别的日志")logger.info("这个是info级别的日志")但是%后面的格式是错误的,在后面读取%号时,读取不到正确的格式。执行行数【42】执行信息[ 这个是info级别的日志 ]Message: '这个是info级别的日志'Message: '这个是info级别
错误搜集
--- Logging error ---
Traceback (most recent call last):
File "D:\Program\Util\py\Lib\logging\__init__.py", line 1160, in emit
msg = self.format(record)
^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 999, in format
return fmt.format(record)
^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 706, in format
s = self.formatMessage(record)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 675, in formatMessage
return self._style.format(record)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 464, in format
return self._format(record)
^^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 460, in _format
return self._fmt % values
~~~~~~~~~~^~~~~~~~
TypeError: not enough arguments for format string
Call stack:
File "C:\Users\Administrator\PycharmProjects\PythonProject4\init_logging.py", line 41, in <module>
logger.info("这个是info级别的日志")
Message: '这个是info级别的日志'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "D:\Program\Util\py\Lib\logging\__init__.py", line 1160, in emit
msg = self.format(record)
^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 999, in format
return fmt.format(record)
^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 706, in format
s = self.formatMessage(record)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 675, in formatMessage
return self._style.format(record)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 464, in format
return self._format(record)
^^^^^^^^^^^^^^^^^^^^
File "D:\Program\Util\py\Lib\logging\__init__.py", line 460, in _format
return self._fmt % values
~~~~~~~~~~^~~~~~~~
TypeError: not enough arguments for format string
Call stack:
File "C:\Users\Administrator\PycharmProjects\PythonProject4\init_logging.py", line 41, in <module>
logger.info("这个是info级别的日志")
Message: '这个是info级别的日志'
Arguments: ()
错误分析

由上图,可以看出应该是format设置格式化器的出现了错误,应该是格式化样式出现了错误
检查代码

进入源码

源码信息
%(name)s Name of the logger (logging channel) %(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL) %(levelname)s Text logging level for the message ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL") %(pathname)s Full pathname of the source file where the logging call was issued (if available) %(filename)s Filename portion of pathname %(module)s Module (name portion of filename) %(lineno)d Source line number where the logging call was issued (if available) %(funcName)s Function name %(created)f Time when the LogRecord was created (time.time() return value) %(asctime)s Textual time when the LogRecord was created %(msecs)d Millisecond portion of the creation time %(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time) %(thread)d Thread ID (if available) %(threadName)s Thread name (if available) %(taskName)s Task name (if available) %(process)d Process ID (if available) %(message)s The result of record.getMessage(), computed just as the record is emitted
可以看到我们使用的格式错误了
再来看

可以看到后面多了一个%
但是%后面的格式是错误的,在后面读取%号时,读取不到正确的格式
%】
解决问题
去除后面的多余的%
运行结果
C:\Users\Administrator\PycharmProjects\PythonProject\.venv\Scripts\python.exe C:\Users\Administrator\PycharmProjects\PythonProject4\init_logging.py
执行时间【2025-07-19 12:29:05,186】 级别【INFO】 执行程序名称【init_logging.py】
执行行数【42】 执行信息[ 这个是info级别的日志 ]Process finished with exit code 0
更多推荐



所有评论(0)