>i 有函数:
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"
但是没有创建带有日志的文件。当我使用函数时,一切都很好。我试图替换配置上的功能,但它不起作用。我哪里有错误?你可以帮我吗?
您的日志文件名不匹配。
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')
配置文件
中的filename
占位符与您传入的logfilename
键不匹配。使它们匹配,它有效。
下面是一个基于您的完整、可运行的示例:
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')
错误的值替换意味着%(filename)s
与任何内容都不匹配。仔细查看,您会发现您传递的默认值logfilename
。
我将配置文件更改为此文件,它可以工作:
[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=('%(logfilename)s', 'a', 'utf8')
[formatter_Formatter]
format=%(asctime)s - %(levelname)s - %(message)s
datefmt="%Y-%m-%d %H:%M:%S"