python通过配置文件记录配置的配置参数“class”正在导致导入

2024-10-01 19:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过扩展TimedRotatingFileHandler类并使用configfile来配置日志记录,从而为我的项目配置日志记录。 configfile很重要,因为我希望能够以这种方式配置日志记录,但是我遇到了导入错误,不知道如何使类名可以从configfile访问

我得到的错误是

File "C:\Python27\lib\logging\config.py", line 160, in _install_handlers
    klass = _resolve(klass)
  File "C:\Python27\lib\logging\config.py", line 101, in _resolve
    __import__(used)
ImportError: No module named rtlogHandler

project/cfg/log_config.ini

[handler_fileHandler]
class=common.rtlogHandler.rtLogFileHandler
level=DEBUG
formatter=logFormatter
args=('rtCompute', 'h', 1, 5)

我的登录类 project/src/common/rtlogHandler.py

import logging
import datetime

# (filename, when='h', interval=1, backupCount=100, encoding=None, delay=False, utc=False)
class rtLogFileHandler(logging.handlers.TimedRotatingFileHandler):
    '''
    Extended to accommodate for current date and custom path for log file name
    '''
    def __init__(self, file_name, rotate_type, rotate_interval, no_of_backup):
        #   use timestamp in filename
        today = datetime.date.today()
        file_name = file_name + '_' + today.strftime('%Y-%m-%d')
        super(rtLogFileHandler, self).__init__(file_name, rotate_type, rotate_interval, no_of_backup)
    def print_me(self):
        print "print me from log file handler "

Tags: nameinpyimportselflogconfiglogging

热门问题