The use of logging in python standard log module

  • 2020-04-02 13:03:39
  • OfStack

Recently write a crawler system, need to use python logging module, so I learned.
The logging system in python's standard library is supported starting with python 2.3. As long as import logging module can be used. If you want to develop a logging system, both output the logs to the console and write them to the log file by using:


import logging
#  To create a logger
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
#  To create a handler For writing to a log file 
fh = logging.FileHandler('test.log')
fh.setLevel(logging.DEBUG)
#  Create another one handler For output to the console 
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
#  define handler Output format 
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
#  to logger add handler
logger.addHandler(fh)
logger.addHandler(ch)
#  Keep a log 
logger.info('foorbar')

In combination with the above example, let's talk about some of the most commonly used apis:
Logging. GetLogger ([name])
Returns an instance of logger or, if no name is specified, root logger. As long as the name is the same, the returned logger instance is the same and there is only one, that is, the name and the logger instance are one-to-one. This means that there is no need to pass logger instances between modules. As long as you know the name, you get the same logger instance.
Logger. SetLevel (LVL)
Set the level of logger. The level has the following levels:

Level order: NOTSET < The DEBUG < The INFO < WARNING < The ERROR < CRITICAL
If the level of looger is set to INFO, logs smaller than INFO will not be output, and logs greater than or equal to INFO will be output

logger.debug("foobar")    #  No output    
logger.info("foobar")        #  The output   
logger.warning("foobar")  #  The output   
logger.error("foobar")      #  The output   
logger.critical("foobar")    #  The output   

Logger. AddHandler (HDLR)
The handler object lets you write the contents of the log to different places. A simple StreamHandler, for example, is where the logs are written to similar files. Python provides more than a dozen practical handlers, some of which are more commonly used:

StreamHandler:  Output to console 
 FileHandler:    Output to file 
BaseRotatingHandler  Can be written to different logs by time. For example, write the log by day to a file at the end of a different date. 
SocketHandler  with TCP Network connection write LOG
DatagramHandler  with UDP Network connection write LOG
SMTPHandler  the LOG written EMAIL mail 

Logging.basicconfig ([**kwargs])* this function is used to configure root logger, create a StreamHandler for root logger, and set the default format. * these functions: Logging.debug (), logging.info(), logging.warning(), logging.error(), logging.critical() will automatically call basicConfig to add a handler* if the root logger already has a handler, This function does nothing using basicConfig to configure the output format and level of the root logger:

import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')

The ogger object provides a logging interface directly. Formatter describes the format of the log. Handler writes the log to a different location, you can save the log to a local file, you can write a log file every hour, and you can transfer the log to another machine via socket.
From the simplest formatter object. Formatter specifies the header for each log record, meaning you can specify the time format of the log record, the process number, the file name, the function name, and so on. You can use this method to create a formatter object:

logging.Formatter.__init__( fmt=None, datefmt=None)

The FMT parameter specifies whether the process number, file name, function name, and other information is present and the format. Datefmt is the date time format, and the default date format is accurate to microseconds, such as' 2003-07-08 16:49:45,896'. Multiple fields can be specified in FMT, each of which is formatted as "%( < The dictionary keys > )s ", for example, you want to print the time, log level, log information can use the following format:

'%(asctime)s - %(levelname)s - %(message)s'

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131101094011.jpg? 201310194044 ">

When logging crawler system logs, it is necessary to define the logging level. The higher the level is, the more detailed the logs are. We can use a dictionary to set different log information corresponding to different levels:

# Save the log level with the dictionary 
format_dict = {
   1 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
   2 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
   3 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
   4 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
   5 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
}

Wrap the code at the beginning of this article in a class

# Developing a logging system,   To output the log to the console,   Also write to the log file    
class Logger():
    def __init__(self, logname, loglevel, logger):
        '''
            Specifies the file path to save the log, the log level, and the calling file 
            Stores the log in the specified file 
        '''

        #  To create a logger
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.DEBUG)

        #  To create a handler For writing to a log file 
        fh = logging.FileHandler(logname)
        fh.setLevel(logging.DEBUG)

        #  Create another one handler For output to the console 
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)

        #  define handler Output format 
        #formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        formatter = format_dict[int(loglevel)]
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        #  to logger add handler
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

    
    def getlog(self):
        return self.logger

A simple logging system is called in the following manner

logger = Logger(logname='log.txt', loglevel=1, logger="fox").getlog()

Related articles: