Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么';我的计时器文件处理器不是在午夜旋转吗?_Python_Logging - Fatal编程技术网

Python 为什么';我的计时器文件处理器不是在午夜旋转吗?

Python 为什么';我的计时器文件处理器不是在午夜旋转吗?,python,logging,Python,Logging,这是我的配置文件: [loggers] keys=root [handlers] keys=TimedRotatingFileHandler [formatters] keys=simpleFormatter [logger_root] level=DEBUG handlers=TimedRotatingFileHandler [handler_TimedRotatingFileHandler] class=handlers.TimedRotatingFileHandler level=

这是我的配置文件:

[loggers]
keys=root

[handlers]
keys=TimedRotatingFileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=TimedRotatingFileHandler

[handler_TimedRotatingFileHandler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('driver.log', 'midnight', 1, 30)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
在我的代码中,我设置并使用以下记录器:

import logging
import logging.config

logging.config.fileConfig('logging.conf')
logging.info('Some message...')
消息被记录到我指定的文件(driver.log)中,但午夜的旋转从未发生


进程必须在午夜运行才能进行轮换吗?这是一个批处理过程,我每15分钟运行一次,但实际上它从来没有在午夜运行。

我想这只会在进程在午夜运行时发生。在您的情况下(cronjob没有运行很长时间),您应该使用一个简单的日志文件,其中当前日期被添加到日志文件名中。这样,“滚动”会自动发生。

答案是流程必须一直运行,才能正常工作

发件人:

旋转应在以下情况下发生: 日志进程创建处理程序 在午夜前做一次记录 之后,为该处理程序指定的调用 午夜


我也遇到了这个问题,由于各种原因,我无法使用rotatelog和cron来旋转日志,只是添加了一个可能出错的额外内容。我使用下面的函数每天旋转文件

import os
import datetime
import glob

def sort_number_ext(s):
    try:
        return int(os.path.splitext(s)[1][1:])
    except:
        return s

def rotate_file(file, keep=30):
    """ Rotate a file if needed. If the file wasn't modified today then we
    rotate it around and remove old files """

    modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(file))

    if modified_date.date() == datetime.datetime.today().date():
        return False

    old_files = glob.glob(file + ".*")
    old_files.sort(key=sort_number_ext, reverse=True)

    for f in old_files:
        try:
            number = int(os.path.splitext(f)[1][1:])
        except ValueError:
            continue

        if number >= keep:
            # If at or above keep limit, remove.
            os.unlink(f)
        else:
            # Increment.
            new = "%s.%s" % (os.path.splitext(f)[0], number + 1)
            os.rename(f, new)

    # Finally rename our log.
    os.rename(file, "%s.1" % file)
    return True

在初始化记录器之前,我调用此函数来旋转日志。

是的,我试图不重新发明轮子,因为我还需要这些文件在一段时间后循环使用。这有利于旋转FileHandler,但不利于人员最初询问的TimedRotatingFileHandler。