SoFunction
Updated on 2025-04-16

Detailed explanation of python logging module and its log timing cleaning method

Python logging module and log timing cleaning

1. Create a logger object

logger = (log_name) # Incomingloggername

Common styles

(level=,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='./Log/test_log.log',
                    filemode='a')
Parameter name describe
level Log output level
format Log output format
datefmt Handlers using specified format string
filename The file name of the log output to the file
filemode File mode, r[+], w[+], a[+]

Log level description:

  • DEBUG: Used when debugging a bug
  • INFO: Used when the program is running normally
  • WARNING: The program is not used as expected, but it is not an error, such as: the user login password is wrong
  • ERROR: Used when an error occurs in the program, such as: IO operation failed
  • CRITICAL: A particularly serious problem, which causes the program to be unable to continue to be used during running. For example, the disk space is empty and is rarely used.

The order of log levels from low to high is: DEBUG < INFO < WARNING < ERROR < CRITICAL

3. Use of commonly used handlers

StreamHandler

Stream handler – One of the three handlers included in the logging module.

Can output log information to, or file-like objects (more precisely, objects that can support write() and flush() methods).

There is only one parameter:

class (stream=None)

Log information will be output to the specified stream, and if the stream is empty, it will be output to the default.

FileHandler

One of the three handlers that come with the logging module. Inherited from StreamHandler. Output log information to disk files.

Construct parameters:

class (filename, mode='a', encoding=None, delay=False)

When the mode defaults to append, delay is true, the file will not be opened until the emit method is executed. By default, log files can be infinitely enlarged.

NullHandler

Empty operating handler, one of the three handlers included in the logging module. No parameters.

WatchedFileHandler

Located in the module. Used to monitor the status of the file. If the file is changed, then close the current stream, reopen the file, and create a new stream. The use of newsyslog or logrotate will cause the file to change.

This handler is specially designed for Linux/Unix systems, because under Windows systems, the files being opened will not be changed.

The parameters are the same as FileHandler:

class (filename, mode='a', encoding=None, delay=False)

RotatingFileHandler

Located in the support loop log file.

class (filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)

The parameters maxBytes and backupCount allow log files to rollover when maxBytes is reached. When the file size reaches or exceeds maxBytes, a new log file will be created.

When either of the two parameters mentioned above is 0, rollover will not occur. That is, there is no maxBytes limit for the file. backupcount is the number of backups, that is, at most, how many backups can be found.

When the current log file size exceeds the set maxBytes, the current log file name example will be added with the suffix of .1, such as .1, and a new file will be created to record log information.

TimedRotatingFileHandler

The timed circular log handler is located, and supports timed generation of new log files.

class (filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)

The parameter when determines the type of time interval, and the parameter interval determines the number of time intervals.

For example, when='D' and interval=2 refer to the two-day interval. The backupCount determines how many log files can be left.

If the number exceeds the number, the old log files will be discarded. When the parameter determines the type of time interval.

Other handlers

SocketHandler、DatagramHandler、SysLogHandler、NtEventHandler、SMTPHandler、MemoryHandler、HTTPHandler

These handlers are not very commonly used, so please refer to the official document for details.

Handler parameter configuration

like:

class (filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
  • when: is a string used to describe the basic unit of the scrolling cycle. The value and meaning of the string are as follows (case insensitive):
type unit
S Second
M point
H hour
D sky
W0-W6 Monday to Sunday
midnight Every morning
  • interval: The scrolling cycle, the unit is specified when, for example: when=’D’, interval=1, which means that a log file is generated every day and the matching expired files are cleaned;
  • backupCount: Indicates the number of retained log files;
  • delay: Delay file creation until the first call to emit() method to create a log file
  • atTime: Create a log file at the specified time (format).

Delete log file settings

# suffix setting will generate a file name xxx.log_file_handler.suffix = "%Y-%m-%"  
# extMatch is a compiled regular expression to match the log file name suffix. It should be noted that suffix and extMatch must match. If it does not match, the expired log will not be deleted.log_file_handler.extMatch = (r"^\d{4}-\d{2}-\d{2}.log$")

The formats of suffix and extMatch must correspond, Year-4 bit, m-2 bit, and so on

For example:

rf_handler = ('', when='midnight', interval=1, backupCount=7, atTime=(0, 0, 0, 0))
rf_handler.setFormatter(("[%(asctime)s] -- %(remote_addr)s -- requested -- %(url)s -- %(levelname)s -- %(process)s -- %(thread)s -- %(module)s -- %(funcName)s -- %(lineno)d -- %(message)s --\n__________"))

Add to the log processing object collection

my_logging.addHandler(handle)

Turn off the log collector

my_logging.removeHandler(handler)

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.