【问题标题】:python logging config file, one per day, not multiplepython日志记录配置文件,每天一个,而不是多个
【发布时间】:2013-06-26 00:47:55
【问题描述】:

我正在尝试创建一个日志配置文件,该文件将每天创建一个完整的日志,但目前它会创建多个文件;

ie. 
readings.log.2013-06-17_01
readings.log.2013-06-17_02
readings.log.2013-06-17_03
readings.log.2013-06-17_04
readings.log.2013-06-17_05
readings.log.2013-06-17_06
readings.log.2013-06-18_01
readings.log.2013-06-18_02
readings.log.2013-06-18_03
readings.log.2013-06-18_04
readings.log.2013-06-18_05
readings.log.2013-06-18_06
...etc

我确定我遗漏了一些东西,但是我需要在我的日志记录配置文件中进行哪些更改以使其每天只创建一个完整的日志文件,无论大小如何!>?

使用 Python 2.7 atm,脚本 24/7 运行

谢谢 马特。

我的日志配置文件; (logging_v3.cfg)

[loggers]
keys=root

[logger_root]
handlers=screen,file
level=NOTSET

[formatters]
keys=simple,complex,logtemps

[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

[formatter_complex]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s

[formatter_logtemps]
format=%(asctime)s %(name)s %(levelname)s %(message)s

[handlers]
keys=file,screen

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=0
formatter=logtemps
level=INFO
args=('logs/readings.log',)

[handler_screen]
class=StreamHandler
formatter=simple
level=DEBUG
args=(sys.stdout,)

以及我在程序中用来完成上述工作的代码; (显然还有很多,但这是日志记录的主要部分)

import logging
import logging.config

logging.config.fileConfig('config/logging_v3.cfg') #logfile config

logging.debug("DEBUG MODE")
logging.debug("INFO MODE")

【问题讨论】:

  • 我不是 100% 确定,但我会尝试将 args=('logs/readings.log',) 更改为 args=('logs/readings.log','midnight',) - 看起来它会按预期传递间隔参数;我不确定您可以在日志记录配置文件中使用哪些键,据我所知,它似乎没有明确记录?
  • 我想这里基本上已经回答了:stackoverflow.com/questions/8467978/…
  • @ernie,试一试。
  • @9000,看到了那个,但不知道如何将它添加到配置文件中,就像我的设置一样。
  • 我对@9​​87654322@ 的阅读表明这是正确的,即“如果backupCount 不为零,则最多保留backupCount 个文件,如果发生翻转时会创建更多文件,则删除最旧的文件。删除逻辑使用间隔来确定要删除哪些文件,因此更改间隔可能会留下旧文件。”您可能还需要在 args 中传递它,例如args=('logs/readings.log', 'midnight', 'backupCount=0') 或类似的东西。

标签: python logging


【解决方案1】:

将 cmets 编码为答案:

logging Configuration file format 的文档似乎有点不清楚每个部分中键和值的有效选项。

对于处理程序,它似乎将参数传递给构造函数,我们需要使用args 键,并在那里指定值,而不是在配置部分使用键值对,例如而不是:

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
args=('logs/readings.log',)

我们应该使用:

[handler_file]
class=handlers.TimedRotatingFileHandler
args=('logs/readings.log', 'midnight',)

【讨论】:

    猜你喜欢
    • 2020-01-03
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多