简体   繁体   English

Python RotatingFileHandler 不旋转

[英]Python RotatingFileHandler not rotating

I try to limit the size of log file in Python, using RotatingFileHandler.我尝试使用 RotatingFileHandler 限制 Python 中日志文件的大小。

I am using the following code:我正在使用以下代码:

logger = logging.getLogger(logging.basicConfig(
    filename = log_filename,
    handlers = [logging.handlers.RotatingFileHandler(filename=log_filename, maxBytes=1024, backupCount=20)],
    format = "%(asctime)s %(message)s",
    level = logging.DEBUG))

I visited here and here .我访问了这里这里 I am using both maxBytes and backupCount parameters, but it is still not working.我同时使用 maxBytes 和 backupCount 参数,但它仍然无法正常工作。 Any idea why the log file is not rotating?知道为什么日志文件没有旋转吗?

Python 2.7 does not support the handlers argument in logging.basicConfig() function. Python 2.7 不支持logging.basicConfig()函数中的handlers参数。 So you have to move the handler out of the basicConfig arguments, like that:因此,您必须将处理程序移出 basicConfig 参数,如下所示:

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(filename=log_filename, maxBytes=1024, backupCount=20)
handler.setFormatter("%(asctime)s %(message)s")
logger.addHandler(handler)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM