簡體   English   中英

python3.6:KeyError:'格式化程序'

[英]python3.6 : KeyError: 'formatters'

我已經在 logging.config 文件中配置了日志記錄。 我創建了一個類,我可以在其中訪問此配置文件、啟用/禁用記錄器並記錄一些信息消息。 我在需要做一些日志記錄的所有模塊中導入這個類。 當我嘗試登錄到文件時,我收到此錯誤消息。 我無法理解此錯誤的含義。

文件“/usr/local/lib/python3.6/configparser.py”,第 959 行,在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") 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM