current position:Home>Python log output logging

Python log output logging

2022-02-02 14:14:07 Ice sky

Python Logging Module Log module

logging Log stream processing

Official tutorial

logging Module log module component

logging The log module of the module contains a total of four components :

  • Loggers Loggers : Provide interfaces that applications can always use , And you can set the generated log level, etc , After the log is generated , It will be handed over to the registered processor for processing

  • processor Handlers : Yes Logger The generated logs are processed —— Set the processing mode through the corresponding function , For example, output format , And the log level of output

  • filter Filters : Better filter the logs to be output to determine the final log results to be output

  • formatter Formatter : Determine the final output format of the log

in fact , In the whole process of an application , adopt Logger Class for log processing and output , These instances are organized through a namespace hierarchy , Between each namespace and subspace, through '.' To separate , for example :scan.text stay scan This Logger In the subspace of , The log name can be defined as needed .

obviously , This naming method can be immediately associated with Python How modules are named , Therefore, a better naming method provided for us in the official documents is through the module __name__ Property to name , It can realize the namespace organization between modules at different levels , It also enables each log output to be associated with the corresponding module , Facilitate log search and management :

logger = logging.getLogger(__name__)

logging Log stream processing flow

Log processing flow of the official website :
 Insert picture description here

therefore , The process of the four components of log stream processing is as follows :

Loggers Logger
filter Filter
processor Handler
formatter Formatter

adopt getLogger Function to get the logger object of this module , And then through setLevel and addHandler Method can set the output level of the log and the processor of the log output for this logger object :

# set log level

# create handler
trf_handler = logging.TimedRotatingFileHandler()

You can also set the corresponding formatter for the created logger , To determine the format of the log finally output to the file :

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to timed rotating file handler

By directly calling different levels of log output functions , You can get the log output of the corresponding format and level :

logger.debug('This is debug')'This is info')
logger.warn('This is warn')
logger.error('This is error')
logger.critical('This is critical')

Loggers Loggers

Handlers processor

Common log processors :

  • FileHandler
  • TimedRotatingFileHandler

Filters filter

Formatter formatter

copyright notice
author[Ice sky],Please bring the original link to reprint, thank you.

Random recommended