[英]Variables in names for python's RotatingFileHandler
I am trying to create a logging system that sorts a specific log into a directory pattern that resembles: logs/<NAME HERE>/<LEVEL>.log
. 我正在尝试创建一个日志记录系统,该系统将特定日志分类为类似于以下目录格式的
logs/<NAME HERE>/<LEVEL>.log
: logs/<NAME HERE>/<LEVEL>.log
。 So far it has not been working out. 到目前为止,它还没有解决。 I wanted to see if I could implement this feature in the logging configuration file instead of programmatically making one for each case.
我想看看是否可以在日志记录配置文件中实现此功能,而不是通过编程为每种情况创建一个功能。
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"simple": {
"format": "%(asctime)s - %(name)s <%(levelname)s>: %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "simple",
"stream": "ext://sys.stdout"
},
"info_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "simple",
"filename": "logs/%(name)s/info.log",
"maxBytes": "10485760",
"backupCount": "20",
"encoding": "utf8"
},
"error_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "simple",
"filename": "logs/%(name)s/errors.log",
"maxBytes": "10485760",
"backupCount": "20",
"encoding": "utf8"
}
},
"loggers": {
"my_module": {
"level": "ERROR",
"handlers": ["console"],
"propagate": "no"
}
},
"root": {
"level": "INFO",
"handlers": ["console", "info_file_handler", "error_file_handler"]
}
}
With the configuration shown above, I get ValueError: Unable to configure handler u'error_file_handler': [Errno 2] No such file or directory: u'.../logs/%(name)s/errors.log'
. 使用上面显示的配置,我得到
ValueError: Unable to configure handler u'error_file_handler': [Errno 2] No such file or directory: u'.../logs/%(name)s/errors.log'
。 So, the approach I took does not support variable names as part of the filename? 因此,我采用的方法不支持将变量名作为文件名的一部分吗? Am I confined to programmatically creating a logging object or is there a way to do this in Python's default logging library?
我是否只能以编程方式创建日志记录对象,还是可以在Python的默认日志记录库中执行此操作? I know that I can create specific loggers for each individual class, but I would really appreciate not requiring myself to manually write out each logger for each case and keep the logging config to unique cases.
我知道我可以为每个单独的类创建特定的记录器,但是我真的很感激不需要我自己为每种情况手动写出每个记录器并使记录配置保持唯一的情况。 I would also like to use the config file if I could.
如果可以的话,我也想使用配置文件。
The logging configuration system allows you to specify handlers that you want to create, but it isn't designed to allow you to create a template for handlers, which seems to be what you want. 日志记录配置系统允许您指定要创建的处理程序,但它并非旨在允许您为处理程序创建模板 ,而这似乎正是您想要的。
I don't know if "name" refers to the logger name, and it doesn't seem like your scheme makes a lot of sense, given how logging levels, loggers and handlers work. 我不知道“名称”是否指的是记录器名称,考虑到记录级别,记录器和处理程序的工作原理,您的方案似乎没有多大意义。 Even if you used a constant name for the log file (getting you around your current problem) you would have both
ERROR
and INFO
level messages in info.log
(as well as WARNING
messages). 即使您为日志文件使用了常量名称(使您了解当前的问题),
info.log
也会有ERROR
和INFO
级别的消息(以及WARNING
消息)。
I wouldn't advise trying to set up separate log files per logger and per level - it's not an especially useful mode of operation, from my experience. 我不建议尝试为每个记录器和每个级别设置单独的日志文件-根据我的经验,这不是一种特别有用的操作模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.