logging in python to scroll logs and delete expired logs

Posted by hyperyoga on Tue, 20 Aug 2019 06:02:15 +0200

From: https://blog.csdn.net/ashi198866/article/details/46725813

The logging library provides two class es for log scrolling (refer to https://docs.python.org/2/library/logging.handlers.html), one is Rotating FileHandler, which scrolls mainly according to the size of the log file, and the other is TimeRotating FileHandler, which scrolls mainly according to time. In practical applications, we usually scroll according to time, so this article mainly introduces the use of TimeRotaingFileHandler (Rotating FileHandler is the same). The code example is as follows:

#!/usr/bin/env python
#_*_coding:utf-8_*_
# vim : set expandtab ts=4 sw=4 sts=4 tw=100 :

import logging
import time
import re
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler

def main():
    #Log Printing Format
    log_fmt = '%(asctime)s\tFile \"%(filename)s\",line %(lineno)s\t%(levelname)s: %(message)s'
    formatter = logging.Formatter(log_fmt)
    #Establish TimedRotatingFileHandler object
    log_file_handler = TimedRotatingFileHandler(filename="ds_update", when="M", interval=2, backupCount=2)
    #log_file_handler.suffix = "%Y-%m-%d_%H-%M.log"
    #log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$")
    log_file_handler.setFormatter(formatter)    
    logging.basicConfig(level=logging.INFO)
    log = logging.getLogger()
    log.addHandler(log_file_handler)
    #Cyclic Printing Log
    log_content = "test log"
    count = 0
    while count < 30:
        log.error(log_content)
        time.sleep(20)
        count = count + 1
    log.removeHandler(log_file_handler)


if __name__ == "__main__":
    main()

 

filename: prefix for log file name;
when: is a string used to describe the basic unit of the rolling cycle. The value and meaning of the string are as follows:
"S": Seconds
"M": Minutes
"H": Hours
"D": Days
"W": Week day (0=Monday)
"midnight": Roll over at midnight
Interval: Scroll cycle, when specified in units, such as: when='D', interval=1, which means that a log file is generated every day;
backupCount: Represents the number of log files retained;
In addition to the above parameters, Timed Rotating FileHandler has two more important member variables, suffix and extMatch, respectively. Suffix refers to the suffix of the log file name. Suffix usually has a formatted time string. Filename and suffix are connected by "..." to form the file name (e.g. filename = "runtime", suffix = "% Y-%m-%d.log", and the generated file is called runtime.2015-07-06.log). ExtMatch is a compiled regular expression that matches the suffix of the log file name. It must match suffix. If suffix and extMatch do not match, the expired log will not be deleted. For example, suffix = "% Y-%m-%d.log", extMatch should only be re.compile(r "^ D {4} - d {2} - d {2}. log $"). By default, when the TimedRotatingFileHandler object is initialized, suffxi and extMatch initialize according to the value of when:

'S': suffix="%Y-%m-%d_%H-%M-%S", extMatch=r"\^d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}"; 
'M':suffix="%Y-%m-%d_%H-%M",extMatch=r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}"; 
'H':suffix="%Y-%m-%d_%H",extMatch=r"^\d{4}-\d{2}-\d{2}_\d{2}"; 
'D':suffxi="%Y-%m-%d",extMatch=r"^\d{4}-\d{2}-\d{2}"; 
'MIDNIGHT':"%Y-%m-%d",extMatch=r"^\d{4}-\d{2}-\d{2}"; 
'W':"%Y-%m-%d",extMatch=r"^\d{4}-\d{2}-\d{2}"; 

If there is no special requirement for log file names, you don't need to set suffix and extMatch. If you need to, you must match them.

Topics: Python vim