Python by TimedRotatingFileHandler cutting logs by time

Online ran a timed script, generated daily log files are written in a file. But the log information can not be output to a single file.
 There are two reasons: 1 performance system log files growing influence of the General Assembly. 2. The log file format is not clear enough, for example, I want to see today's log, log information is not easy today to find (even for a time log output do Tips)

Log weekly (W), day (D), when (H), minutes (M), seconds (S) for cutting by providing TimedRotatingFileHandler.

 Look at a simple example:

import time
import logging
import os
from logging import handlers


def _logging(**kwargs):
    level = kwargs.pop('level', None)
    filename = kwargs.pop('filename', None)
    datefmt = kwargs.pop('datefmt', None)
    format = kwargs.pop('format', None)
    if level is None:
        level = logging.DEBUG
    if filename is None:
        filename = 'default.log'
    if datefmt is None:
        datefmt = '%Y-%m-%d %H:%M:%S'
    if format is None:
        format = '%(asctime)s [%(module)s] %(levelname)s [%(lineno)d] %(message)s'

    logging.getLogger = log (filename)
    format_str = logging.Formatter (format, datefmt)
    # number backupCount save the log, expired automatically deleted
    # when segmentation by what date format (used here to facilitate the testing sec)
    TH = handlers.TimedRotatingFileHandler ( filename = filename, When = 'S', BACKUPCOUNT =. 3, encoding = 'UTF-. 8')
    th.setFormatter (format_str)
    th.setLevel (logging.info)
    log.addHandler (TH)
    log.setLevel (Level)
    return log

os.makedirs("./logs", exist_ok=True)
logger = _logging(filename='./logs/default.log')

 

if __name__ == '__main__':
    while True:
        time.sleep(0.1)
        logger.info('哈哈哈')

The results are as follows:

The code can be normal operation, but also generate a fixed log number, but there is a problem, the generated log file format your file name + time format, there is no set time, then the default settings to the second (here in seconds cleavage )
modify the log format suffix name:

Was added in the above code #
DEF Namer (filename):
    return filename.split ( 'default.')
Th.namer Namer =
# is set to S, the default suffix for the Y-% m-% d_% H-% M-% S
th.suffix = "D_%%% Y-H-M-%%% M-S.log"

# To see more visual effects, may be displayed on the console agreed
cmd = logging.StreamHandler ()
cmd.setFormatter (format_str)
cmd.setLevel (Level)
log.addHandler (cmd)

operation result:

Name seems to be, but does not seem to serve the purpose of the log automatically deleted ah, but did not in the previous log folder.
 To look at the source:

    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))
        if len(result) < self.backupCount:
            result = []
        else:
            result.sort()
            result = result[:len(result) - self.backupCount]
        return result


It is logical to delete it, the key is passed. Determines whether to repeat the foregoing fields, beginning after deleting the specific number of repetitions.
 So the question is, either themselves to rewrite the source code, or can only use default. .Log date of this format.
 Usually use the logging code attached

import logging
import os
from logging import handlers


def _logging(**kwargs):
    level = kwargs.pop('level', None)
    filename = kwargs.pop('filename', None)
    datefmt = kwargs.pop('datefmt', None)
    format = kwargs.pop('format', None)
    if level is None:
        level = logging.DEBUG
    if filename is None:
        filename = 'default.log'
    if datefmt is None:
        datefmt = '%Y-%m-%d %H:%M:%S'
    if format is None:
        format = '%(asctime)s [%(module)s] %(levelname)s [%(lineno)d] %(message)s'

    log = logging.getLogger(filename)
    format_str = logging.Formatter(format, datefmt)

    def namer(filename):
        return filename.split('default.')[1]

    # cmd = logging.StreamHandler()
    # cmd.setFormatter(format_str)
    # cmd.setLevel(level)
    # log.addHandler(cmd)

    os.makedirs("./debug/logs", exist_ok=True)
    th_debug = handlers.TimedRotatingFileHandler(filename="./debug/" + filename, when='D', backupCount=3,
                                                encoding='utf-8')
    # th_debug.namer = namer
    th_debug.suffix = "%Y-%m-%d.log"
    th_debug.setFormatter(format_str)
    th_debug.setLevel(logging.DEBUG)
    log.addHandler(th_debug)

    th = handlers.TimedRotatingFileHandler(filename=filename, when='D', backupCount=3, encoding='utf-8')
    # th.namer = namer
    th.suffix = "%Y-%m-%d.log"
    th.setFormatter(format_str)
    th.setLevel(logging.INFO)
    log.addHandler(th)
    log.setLevel(level)
    return log


os.makedirs('./logs', exist_ok=True)
logger = _logging(filename='./logs/default')

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159450.htm