I am working on implementing logging in my Python project and hit a bit. I am trying to configure my log so that Handlers and Formatters are all organized in a configuration file. What I'm trying to do at the moment is to configure mine fileHandlerso that it creates a log file that looks something like this: YYYY_MM_DD.logobviously with Y representing the year, M representing the month, and D representing the day.
Here is what I did with my configuration file:
[loggers]
keys=root,MainLogger
[handlers]
keys=fileHandler, consoleHandler
[formatters]
keys=logFormatter, consoleFormatter
[logger_root]
level=DEBUG
handlers=fileHandler
[logger_MainLogger]
level=DEBUG
handlers=fileHandler, consoleHandler
qualname=MainLogger
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=consoleFormatter
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=logFormatter
args=(datetime.now().strftime('%Y_%m_%d.log'), 'a')
[formatter_logFormatter]
format=%(asctime)s | %(levelname)-8s | %(lineno)04d | %(message)s
[formatter_consoleFormatter]
format=%(asctime)s | %(levelname)-8s | %(fillname)s-%(funcName)s-%(lineno)04d | %message)s
The file I use to verify the configuration is pretty simple:
import logging
import logging.config
logging.config.fileConfig('logging.conf')
logger = logging.getLogger('MainLogger')
logger.debug("TEST")
The specific error that I am currently receiving is:
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: "%Y_%m_%d.log'), 'a')"
%Y, %m %d, , . , , ?
, test.log, , , , , .