Python日志记录-将日期设置为文件名 [英] Python Logging - Set Date as Filename
问题描述
我正在努力在Python项目中实现日志记录,但遇到了一些麻烦.我试图设置日志记录,以便将处理程序和格式化程序全部组织到一个配置文件中.目前,我正在尝试设置fileHandler
,以便它将创建一个类似于以下内容的日志文件:YYYY_MM_DD.log
显然,Y代表年份,M代表月份,D代表一天.
I am working on implementing logging within my Python project and have hit a bit of a snag. I am trying to set up my logging such that the Handlers, and Formatters are all organized into a configuration file. What I am trying to do at the moment is to set up my fileHandler
such that it will create a log file that looks something like this: YYYY_MM_DD.log
obviously with the Y's representing the year, M's representing the month, and D's representing the day.
这是我尝试使用配置文件进行的操作:
This is what I have attempted with my config 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 am using to test 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 I am getting at the moment is:
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: "%Y_%m_%d.log'), 'a')"
我已经尝试按照错误提示更改%Y
,%m
和%d
,但这不能解决问题.我该如何设置配置文件,以使我的日志文件看起来像我希望的样子?
I've tried changing the %Y
, %m
, and %d
as the error says, but that doesn't fix the problem. How do I go about setting up the config file so that my log files look the way I want them to?
当我将文件名更改为test.log
时,我应该注意一切正常,因此这似乎是我唯一遇到的错误.
I should note when I change the filename to test.log
everything worked fine, so this is the only error I seem to be having with this.
推荐答案
您无法在配置文件中使用datetime
,因为它不知道这意味着什么.但是,您可以在python文件本身中添加Filehandler
:
You can't use datetime
in a config file, as it doesn't know what it means. You can however add the Filehandler
in the python file itself:
import logging.config
from datetime import datetime
logging.config.fileConfig('aaa.conf')
logger = logging.getLogger('MainLogger')
fh = logging.FileHandler('{:%Y-%m-%d}.log'.format(datetime.now()))
formatter = logging.Formatter('%(asctime)s | %(levelname)-8s | %(lineno)04d | %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.debug("TEST")
这样,您可以在处理程序中将日期设置为文件名.
This way you can set the date as the file name in the handler.
这是配置文件,请注意,您在最后一个格式化程序中输入了错字,您将fillname
代替了filename
,并且在message
中忘记了(
.
This is the config file, note that you had a typo in the last formatter, you put fillname
instead of filename
and you forgot (
in message
.
[loggers]
keys=root,MainLogger
[handlers]
keys=consoleHandler
[formatters]
keys=consoleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_MainLogger]
level=DEBUG
handlers=consoleHandler
qualname=MainLogger
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=consoleFormatter
args=(sys.stdout,)
[formatter_consoleFormatter]
format=%(asctime)s | %(levelname)-8s | %(filename)s-%(funcName)s-%(lineno)04d | %(message)s
这应该工作正常.
这篇关于Python日志记录-将日期设置为文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!