Python中的配置文件,用于记录函数

时间:2017-10-06 18:11:18

标签: 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(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"

但是没有创建带有日志的文件。当我使用功能时,一切都很好。我试图替换配置上的功能,但它不起作用。哪里有错误?你能救我吗?

1 个答案:

答案 0 :(得分:0)

配置文件中的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"