我使用 python 的日志记录模块设置了一个 python 记录器。我想使用 ConfigParser 模块将我与日志记录 Formatter 对象一起使用的字符串存储在配置文件中。
格式字符串存储在一个单独文件中的设置字典中,该文件处理配置文件的读取和写入。我遇到的问题是 python 仍然尝试格式化文件并在读取所有特定于日志记录模块的格式化标志时失败。
{
"log_level":logging.debug,
"log_name":"C:\\Temp\\logfile.log",
"format_string":
"%(asctime)s %(levelname)s: %(module)s, line %(lineno)d - %(message)s"
}
我的问题很简单:如何在此处禁用格式化功能,同时将其保留在其他地方。我最初的反应是大量使用反斜杠来转义各种百分比符号,但这当然会永久破坏格式,即使我需要它也不会工作。
我还应该提到,因为它在评论中被收购了,ConfigParser 做了一些内部插值,导致跳闸。这是我的回溯:
Traceback (most recent call last):
File "initialconfig.py", line 52, in <module>
"%(asctime)s %(levelname)s: %(module)s, line %(lineno)d - %(message)s"
File "initialconfig.py", line 31, in add_settings
self.set(section_name, setting_name, default_value)
File "C:\Python26\lib\ConfigParser.py", line 668, in set
"position %d" % (value, m.start()))
ValueError: invalid interpolation syntax in '%(asctime)s %(levelname)s: %(module
)s, line %(lineno)d - %(message)s' at position 10
此外,关于良好设置文件实践的一般指针会很好。这是我第一次用 ConfigParser 做任何重要的事情(或日志记录)。
在此先感谢,多米尼克