[英]Configuration file in Python for logging instead function
i have function: 我有功能:
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
I tried to replace on method: 我试图替换方法:
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")
My config: 我的配置:
[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"
But file with log, was not created. 但是未创建带有日志的文件。 When i am using function, everything is ok.
当我使用函数时,一切正常。 I tried to replace function on config and it does not work.
我试图替换config上的功能,但它不起作用。 Where i have error ?
我哪里出错了? Can you help me ?
你能帮助我吗 ?
Your filename
placeholder in the config file doesn't match the logfilename
key you're passing in. Make them match, and it works. 您在配置文件中的文件
filename
占位符与您要传递的logfilename
密钥不匹配。使它们匹配即可。
Here's a full, runnable example based on yours: 这是一个基于您的完整的可运行示例:
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!')
When I run that with your config file, I get this error: 当我用您的配置文件运行它时,出现此错误:
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')
Bad value substitution means that %(filename)s
doesn't match anything. 错误的值替换意味着
%(filename)s
不匹配任何内容。 Look carefully, and you see that the defaults you passed in use logfilename
. 仔细查看,您会看到传入的默认值是使用
logfilename
。
I change the config file to this, and it works: 我将配置文件更改为此,并且它可以正常工作:
[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"
Your log file name does not match. 您的日志文件名不匹配。
logging.config.fileConfig(config_file, defaults={'logfilename': logfilename}, disable_existing_loggers=False)
>>{'logfilename': logfilename}
And
>>args=('%(filename)s', 'a', 'utf8')
Your file handler should be like this 您的文件处理程序应该是这样的
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=Formatter
args=('%(logfilename)s', 'a', 'utf8')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.