gpt4 book ai didi

python3.6 : KeyError: 'formatters'

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:55 28 4
gpt4 key购买 nike

我已经在logging.config文件中配置了日志记录。我创建了一个类,在其中访问此配置文件、启用/禁用记录器并记录一些信息消息。我将此类导入到需要进行日志记录的所有模块中。当我尝试记录到文件时,收到此错误消息。我无法理解这个错误的含义。

File "/usr/local/lib/python3.6/configparser.py", line 959, in getitem raise KeyError(key) KeyError: 'formatters'

logging.config
[loggers]
keys=root

[handlers]
keys=fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=fileHandler

[handler_fileHandler]
class=FileHandler
level=INFO
formatter=simpleFormatter
args=('example.log','a')

[formatter_simpleFormatter]
class=logging.Formatter
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

#Log.py
import logging.config
class Monitor(object):

fileName = path.join(path.split(path.dirname(path.abspath(__file__)))[0], "logging.config")
print (fileName) #prints /usr/local/lib/python3.6/site-packages/myproject-0.0.1-py3.6.egg/MyPackageName/logging.config
logging.config.fileConfig(fileName)
logger = logging.getLogger('root')
logger.disabled = False

@staticmethod
def Log(logMessage):
Monitor.logger.info(logMessage)

#sub.py
import Monitor
class Example

def simplelog(self,message):
Monitor.Log("Logging some message here")
#call some function here
Monitor.Log("Logging some other messages here for example")

最佳答案

当我尝试从不在项目根目录上的 python 脚本加载配置时,我遇到了类似的问题。我发现的是:

logging.config.fileConfig 依赖于 configparser并且在使用绝对路径初始化时遇到问题。尝试相对路径。

替换

    fileName = path.join(path.split(path.dirname(path.abspath(__file__)))[0], "logging.config") 

像这样的东西:

    ## get path tree from project root and replace children from root with ".."
path_rslv = path.split(path.dirname(path.abspath(__file__)))[1:]
fileName = path.join(*[".." for dotdot in range(len(path_rslv)], "logging.config")

关于python3.6 : KeyError: 'formatters' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636747/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com