Python中用于记录函数的配置文件
我有以下职能:Python中用于记录函数的配置文件,python,logging,configuration,config,journal,Python,Logging,Configuration,Config,Journal,我有以下职能: def go_logger(logfilename): formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') logging.basicConfig(filemode='a', datefmt='%m-%d-%Y %H:%M:%S') logger = logging.getLogger(logfilename) logger.setLevel(logg
def go_logger(logfilename):
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
logging.basicConfig(filemode='a', datefmt='%m-%d-%Y %H:%M:%S')
logger = logging.getLogger(logfilename)
logger.setLevel(logging.DEBUG)
handler = closehandler.ClosingHandler(os.path.join('/path/to/my/logs', logfilename),
mode='a', encoding='utf-8')
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
我尝试在方法上替换:
def get_logger(logfilename):
config_file = ('/path/to/my/config')
logging.config.fileConfig(config_file, defaults={'logfilename': logfilename}, disable_existing_loggers=False)
logger = logging.getLogger("main")
我的配置:
[loggers]
keys=root
[handlers]
keys=fileHandler
[formatters]
keys=Formatter
[logger_root]
level=DEBUG
handlers=fileHandler
qualname=main
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=Formatter
args=('%(filename)s', 'a', 'utf8')
[formatter_Formatter]
format=%(asctime)s - %(levelname)s - %(message)s
datefmt="%Y-%m-%d %H:%M:%S"
但未创建包含日志的文件。当我使用函数时,一切都正常。我试图替换config上的函数,但它不起作用。我哪里有错误?您能帮助我吗?您的配置文件中的
文件名
占位符与您传入的日志文件名
键不匹配。让它们匹配,它就会起作用
以下是一个基于您的完整、可运行的示例:
import logging.config
def get_logger(logfilename):
config_file = ('config.txt')
logging.config.fileConfig(config_file, defaults={'logfilename': logfilename}, disable_existing_loggers=False)
logger = logging.getLogger("main")
return logger
logger = get_logger('scratch.log')
logger.info('Hello, World!')
当我使用您的配置文件运行时,会出现以下错误:
Traceback (most recent call last):
File "/home/don/.IdeaIC2017.2/config/scratches/scratch.py", line 10, in <module>
logger = get_logger('scratch.log')
File "/home/don/.IdeaIC2017.2/config/scratches/scratch.py", line 6, in get_logger
logging.config.fileConfig(config_file, defaults={'logfilename': logfilename}, disable_existing_loggers=False)
File "/usr/lib/python2.7/logging/config.py", line 85, in fileConfig
handlers = _install_handlers(cp, formatters)
File "/usr/lib/python2.7/logging/config.py", line 161, in _install_handlers
args = cp.get(sectname, "args")
File "/usr/lib/python2.7/ConfigParser.py", line 623, in get
return self._interpolate(section, option, value, d)
File "/usr/lib/python2.7/ConfigParser.py", line 669, in _interpolate
option, section, rawval, e.args[0])
ConfigParser.InterpolationMissingOptionError: Bad value substitution:
section: [handler_fileHandler]
option : args
key : filename
rawval : ('%(filename)s', 'a', 'utf8')
您的日志文件名不匹配
logging.config.fileConfig(config_file, defaults={'logfilename': logfilename}, disable_existing_loggers=False)
>>{'logfilename': logfilename}
And
>>args=('%(filename)s', 'a', 'utf8')
您的文件处理程序应该如下所示
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=Formatter
args=('%(logfilename)s', 'a', 'utf8')
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=Formatter
args=('%(logfilename)s', 'a', 'utf8')