Reputation: 637
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. Where i have error ? Can you help me ?
Upvotes: 2
Views: 7427
Reputation: 374
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')
Upvotes: 1
Reputation: 56230
Your filename
placeholder in the config file doesn't match the logfilename
key you're passing in. Make them match, and it works.
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. Look carefully, and you see that the defaults you passed in use 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"
Upvotes: 0