SoFunction
Updated on 2025-03-04

Python uses custom time scrolling log processor

Logging is an integral part of the application development process. It helps developers track the operation of the application, diagnose problems, and collect other useful information. To manage log files more efficiently, we can use Python'sloggingmodule and combine a custom log processor to scroll log files by time.

This tutorial will explain how to create a log manager (LogManager), which uses a custom time scrolling log processor (CustomTimedRotatingFileHandler) to manage log files. This processor will scroll the log files based on time (e.g. daily) and keep a certain number of old log files as backups.

Step 1: Install Python environment

Make sure that Python is installed on your computer. This tutorial is suitable for Python versions.

Step 2: Create a custom time scroll log processor

First, we need to create an inheritedTimedRotatingFileHandlercustom processor. This processor will be rewritedoRollovermethod to handle daylight saving time changes (although the examples in this tutorial do not implement specific daylight saving time processing logic, corresponding comments and hints are provided).

class CustomTimedRotatingFileHandler(TimedRotatingFileHandler):
    def doRollover(self):
        # If there is currently an open log file stream, close it        if :
            ()
             = None

        # Get the timestamp of the current time (seconds)        currentTime = int(())

        # Get whether the current time is daylight saving time (dstNow represents the current daylight saving time state, 1 is daylight saving time, 0 is non-daylight saving time, and -1 is invalid information)        dstNow = (currentTime)[-1]

        # Calculate the time stamp of the previous scroll time        t =  - 

        # Decide to use gmtime or localtime to get time tuples based on whether UTC time is used        if :
            timeTuple = (t)
        else:
            timeTuple = (t)
            # Get whether the previous scrolling time was daylight saving time            dstThen = timeTuple[-1]

            # If the current daylight saving time state is different from the daylight saving time state of the previous scroll time            if dstNow != dstThen:
                # If it is daylight saving time now, there is no operation here (pass)                if dstNow:
                    pass
                # If it is not daylight saving time now, there is no operation here (pass)                else:
                    pass
                # Note: Logic for handling daylight saving time changes should be added here, such as adjusting timestamps to reflect the impact of daylight saving time changes.
        # Generate the file name of the new log file (based on the basic file name and scrolling rules)        dfn = self.rotation_filename( + ".")

        # If the new log file does not exist, call the rotate method to create it        if not (dfn):
            (, dfn)

        # If the maximum number of backup files is set, delete old files that exceed this number        if  > 0:
            for s in ():
                (s)

        # If delay flag is False, a new log file stream is opened to continue logging        if not :
             = self._open()

        # Calculate the time stamp of the next scroll time        newRolloverAt = (currentTime)

        # Make sure the next scroll time is greater than the current time, if not, add a scroll interval        while newRolloverAt <= currentTime:
            newRolloverAt = newRolloverAt + 

        # Update rolloverAt attribute to the time stamp of the next scroll time         = newRolloverAt

Step 3: Create a Log Manager

Next, we will create aLogManagerClass, which is responsible for configuring and starting the logger and provides methods for logging.

class LogManager:
    def __init__(self, log_dir, log_name):
        """
         Initialize the log manager
         :param log_dir: directory where log file is stored
         :param log_name: The basic name of the log file, the date will be appended to the file name
         """
        self.log_dir = log_dir
        self.log_name = log_name
        self.setup_logger()

    def setup_logger(self):
        """
         Configure and start the logger
         """
        # Make sure the log directory exists        if not (self.log_dir):
            (self.log_dir)

        # Create a logger         = (self.log_name)
        ()  # You can adjust the log level as needed
        # Create a handler for writing to log files and scrolling once a day        log_file_path = (self.log_dir, self.log_name + ".log")
        handler = CustomTimedRotatingFileHandler(log_file_path, when="midnight", backupCount=3650)

        # Create log format        formatter = ('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        (formatter)

        # Add handler to logger        (handler)

    def debug(self, message):
        """
         Record debugging information
         """
        (message)

    def info(self, message):
        """
         Record ordinary information
         """
        (message)

    def warning(self, message):
        """
         Record warning information
         """
        (message)

    def error(self, message):
        """
         Record error information
         """
        (message)

    def critical(self, message):
        """
         Record critical error messages
         """
        (message)

Step 4: Use the Log Manager

Now we can useLogManagerCome to record the log. Here is a simple example showing how to create a log manager and record log information at different levels.

import datetime

# Specify the directory and basic name of the log file stored in the log filelog_dir = './logs'
log_name = 'application'

# Create a log manager instancelog_manager = LogManager(log_dir, log_name)

# Record log information at different levelslog_manager.debug(f"Debug message at {()}")
log_manager.info(f"Info message at {()}")
log_manager.warning(f"Warning message at {()}")
log_manager.error(f"Error message at {()}")
log_manager.critical(f"Critical message at {()}")

Step 5: View the log file

After running the above code, you should see a new log file in the specified log directory (e.g.), and old log files generated based on time (if any). You can open these files to view the log information recorded.

Things to note

  • Daylight saving time processing: In this tutorialCustomTimedRotatingFileHandlerClasses provide comments and hints for handling daylight saving time changes, but do not implement specific logic. In practical applications, you may need to write code to handle daylight saving time changes according to your needs.
  • Log Level: You can adjust the log level of the logger as needed. For example, if you want to log only errors and warning messages, you can set the log level to
  • Log format: You can customize the log format to meet your needs. For example, you can add more context information, timestamp formats, etc.
  • Performance considerations: In high-performance applications, frequent logging may have a performance impact. Therefore, make sure to configure the logger properly and use log levels and scrolling policies with caution.

By following this tutorial, you should be able to successfully create a log manager and use a custom time scrolling log processor to manage your log files. This will help you track and diagnose problems in your application more effectively.

This is the end of this article about Python using custom time scrolling log processor. For more related content of Python time scrolling log processor, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!