Solution to the Problem of not deleting python logging log rotation files,
Preface
Recently, I am maintaining the python project code of the project. The project uses the python Log Module logging and sets the number of logs to be saved. However, the log does not take effect yet. You also need to regularly clean up data through contab.
Analysis
The project uses the TimedRotatingFileHandler of logging:
#!/user/bin/env python# -*- coding: utf-8 -*-import loggingfrom logging.handlers import TimedRotatingFileHandlerlog = logging.getLogger()file_name = "./test.log"logformatter = logging.Formatter('%(asctime)s [%(levelname)s]|%(message)s')loghandle = TimedRotatingFileHandler(file_name, 'midnight', 1, 2)loghandle.setFormatter(logformatter)loghandle.suffix = '%Y%m%d'log.addHandler(loghandle)log.setLevel(logging.DEBUG)log.debug("init successful")
Refer to the official python logging documentation:
Https://docs.python.org/2/library/logging.html
View its entry instance, and you can see the time-based rotation:
import logging# create loggerlogger = logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler()ch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')
You can't see anything wrong.
Take a look at the logging code and find the content related to TimedRotatingFileHandler. Delete the content of the expired log:
Logging/handlers. py
def getFilesToDelete(self): """ Determine the files to delete when rolling over. More specific than the earlier method, which just used glob.glob(). """ dirName, baseName = os.path.split(self.baseFilename) fileNames = os.listdir(dirName) result = [] prefix = baseName + "." plen = len(prefix) for fileName in fileNames: if fileName[:plen] == prefix: suffix = fileName[plen:] if self.extMatch.match(suffix): result.append(os.path.join(dirName, fileName)) result.sort() if len(result) < self.backupCount: result = [] else: result = result[:len(result) - self.backupCount] return result
The principle of rotation deletion is to find the files matching the suffix in the log directory and add them to the deletion list. If the number of files exceeds the specified number, add them to the list to be deleted, let's look at the matching principle:
elif self.when == 'D' or self.when == 'MIDNIGHT': self.interval = 60 * 60 * 24 # one day self.suffix = "%Y-%m-%d" self.extMatch = r"^\d{4}-\d{2}-\d{2}$"
ExMatch is a regular expression matching. The format is-separated time, and we set a new suffix with no-separator:
Loghandle. suffix = '% Y % m % d'
In this way, the file to be deleted is not found and related logs are not deleted.
Summary
1. Use public interfaces as much as possible for encapsulated libraries. Do not modify internal variables at will;
2. If there is a problem with the code, you can check the code.