简体   繁体   English

Python记录器:限制日志文件似乎不限制我的文件

[英]Python logger: limit log file seems not limit my file

So i want to limit my log file. 所以我想限制我的log文件。

import logging
import colorlog
from logging.handlers import RotatingFileHandler

def init_logger(dunder_name, testing_mode) -> logging.Logger:
    log_format = '[%(asctime)s]: [%(levelname)s]: %(message)s'

    bold_seq = '\033[1m'
    colorlog_format = (
        f'{bold_seq} '
        '%(log_color)s '
        f'{log_format}'
    )

    logFile = 'app.log'
    colorlog.basicConfig(format=colorlog_format)

    handler = RotatingFileHandler(logFile, mode='a', maxBytes=50, backupCount=0, encoding=None, delay=0)
    logger = logging.getLogger(dunder_name)

    if testing_mode:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    formatter = logging.Formatter(log_format)
    handler.setFormatter(formatter)
    logger.addHandler(handler)


    return logger

And after define maxBytes=50 i cen see that my log file continue to growing ( 4MB at this moment) 在定义maxBytes=50之后,我看到我的日志文件继续增长(此刻为4MB

What i doing wrong ? 我做错了什么?

Because backupCount is 0 . 因为backupCount0

Rollover occurs whenever the current log file is nearly maxBytes in length; 每当当前日志文件的长度接近maxBytes时,就会发生翻转。 but if either of maxBytes or backupCount is zero, rollover never occurs, so you generally want to set backupCount to at least 1, and have a non-zero maxBytes. 但是如果maxBytes或backupCount中的任何一个为零,则永远不会发生过渡,因此通常需要将backupCount设置为至少1,并且maxBytes为非零。

https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler

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

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