SoFunction
Updated on 2024-11-19

python (logging) Log rollback operations by date and size

Description: Logs are rolled back by date and size.

Code:

# -*- coding: utf-8 -*-
import os
import  
log_dir = ((__file__)) +  + 'logs' 
if not (log_dir):
 (log_dir) 
# CONSTANT VARIABLES 
MODULE_NAME = 'my_module'
LOG_LEVEL = 'INFO' 
def get_logger(module_name=MODULE_NAME, log_level=LOG_LEVEL): 
 () 
 logger = (module_name) 
 (log_level)
 
 # # Rollback by time 1 change per day, retained for 180 days
 # time_file_handler = (
 # log_dir +  + module_name + '_day.log',
 # when='midnight',
 # interval=1,
 # backupCount=180
 # )
 #
 # time_file_handler.suffix = '%Y-%m-%' # by day
 time_file_handler = (
 log_dir +  + module_name + '_sec.log',
 when='S',
 interval=1,
 backupCount=180
 ) 
 time_file_handler.suffix = '%Y-%m-%d_%H-%M-%' # Press the second
 formatter = ('[%(asctime)s]-[%(filename)s]-[%(funcName)s]-[%(lineno)d]-12s: [%(levelname)s]-8s>> %(message)s')
 time_file_handler.setFormatter(formatter) 
 (time_file_handler)
 
 # # Rollback by size
 # file_size_handler = (
 # log_dir +  + module_name + '',
 # maxBytes=1024,
 # backupCount=1000,
 # )
 # file_size_handler.setFormatter(formatter)
 # (file_size_handler) 
 return logger 
if __name__ == '__main__':
 logger = get_logger()
 ('hello')

Output:

(1) Logs:

[2019-12-21 14:12:44,682]-[paper4_logging.py]-[<module>]-[59]-12s: [INFO]-8s>> hello

(2) Catalog:

Saturday, 2019/12/21 14:12 86 my_module_sec.log

Saturday, 2019/12/21 14:12 86 my_module_sec.log.2019-12-21_14

Saturday, 2019/12/21 14:12 86 my_module_sec.log.2019-12-21_14

Saturday, 2019/12/21 14:12 86 my_module_sec.log.2019-12-21_14

Supplementary: log scrolling and expired log deletion with logging in python

The logging library provides two classes that can be used for log scrolling, one is RotatingFileHandler, which mainly scrolls based on the size of the log file, and the other is TimeRotatingFileHandler, which mainly scrolls based on the time.

In practice, we usually scroll according to the time, therefore, in this article, we mainly introduce the use of TimeRotaingFileHandler (RotatingFileHandler 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  import TimedRotatingFileHandler
from  import RotatingFileHandler
def main():
 # Log Print Format
 log_fmt = '%(asctime)s\tFile \"%(filename)s\",line %(lineno)s\t%(levelname)s: %(message)s'
 formatter = (log_fmt)
 #CreateTimedRotatingFileHandler object
 log_file_handler = TimedRotatingFileHandler(filename="ds_update", when="M", interval=2, backupCount=2)
 #log_file_handler.suffix = "%Y-%m-%d_%H-%"
 #log_file_handler.extMatch = (r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$")
 log_file_handler.setFormatter(formatter) 
 (level=)
 log = ()
 (log_file_handler)
 # Cyclic printing of logs
 log_content = "test log"
 count = 0
 while count < 30:
 (log_content)
 (20)
 count = count + 1
 (log_file_handler)
if __name__ == "__main__":
 main()

filename:The prefix of the log file name;

when:is a string that describes the basic unit of the scroll 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:Rolling period, the unit has when specified, e.g. when='D',interval=1, which means that a log file is generated every day;

backupCount:Indicates the number of reservations in the log file;

In addition to the above parameters, TimedRotatingFileHandler has two more important member variables, which are suffix and extMatch.

suffix is the suffix of the log file name, suffix usually with a formatted time string, filename and suffix by "." Connection of filename and suffix by "." constitutes the file name (for example: filename="runtime", suffix="%Y-%m-%", the resulting file name is runtime.).

extMatch is a compiled regular expression to match the suffix of the log file name, it must be matched with suffix, if suffix and extMatch don't match, the expired log will not be deleted.

For example, suffix="%Y-%m-%", extMatch should only be (r"^\d{4}-\d{2}-\d{2}.log$").

By default, when the TimedRotatingFileHandler object is initialized, suffxi and extMatch are initialized based on 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 are no special requirements for log filenames, you can leave suffix and extMatch unset; if you need them, make sure they match.

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to advise me.