The python log is output to the screen, and the python log is written to the file

The python log is output to the screen, and the python log is written to the file

Log

Logs are a way to track events that occur while the software is running. The software developer calls the log function in the code to indicate that a specific event has occurred. The event is described by a descriptive message, which can optionally contain variable data (ie, data that is potentially different for each occurrence of the event). An event also has the importance that the developer attributed to the event; the importance can also be called the level or severity.

logging provides a set of convenient functions for simple logging. They are debug(), info(), warning(), error() and critical().

The logging functions are named according to the level or severity of the events they are used to track. The standard levels and their applicability are described as follows (in order of increasing severity):

level When to use
DEBUG The detailed information is generally only used when debugging a problem.
INFO Prove that things are working as expected.
WARNING Some hints of unexpected events, or hints of problems that may occur in the future. For example: insufficient disk space. But the software will still run as usual.
ERROR Due to more serious problems, the software can no longer perform some functions.
CRITICAL A serious error indicates that the software can no longer run.
level Numeric value
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0
 

The default level is WARNING, which means that only those at this level and above will give feedback, unless the logging module is used for other things.

Tracked events can be handled in different ways. The easiest way to deal with them is to print them out on the console. Another common method is to write to a disk file.

One, print to the console

import logging
logging.debug('debug 信息')
logging.warning('只有这个会输出。。。')
logging.info('info 信息')

Since the default level is warning, all warning messages will be output to the console.

Use logging.basicConfig() to print information to the console:

import logging
logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', level=logging.DEBUG)
logging.debug('debug 信息')
logging.info('info 信息')
logging.warning('warning 信息')
logging.error('error 信息')
logging.critical('critial 信息')

Since the value of level in logging.basicConfig() is set to logging.DEBUG, all debug, info, warning, error, critical logs will be printed to the console.

日志级别: debug < info < warning < error < critical
logging.debug('debug级别,最低级别,一般开发人员用来打印一些调试信息')
logging.info('info级别,正常输出信息,一般用来打印一些正常的操作')
logging.warning('waring级别,一般用来打印警信息')
logging.error('error级别,一般用来打印一些错误信息')
logging.critical('critical 级别,一般用来打印一些致命的错误信息,等级最高')

So if you set level = logging.info(), the debug information will not be output to the console.

Second, use logging.basicConfig() to save the log to a file

import logging
logging.basicConfig(level=logging.DEBUG,  # 控制台打印的日志级别
                    filename='new.log',  # log文件名字
                    filemode='a',  # 模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志,a是追加模式,默认如果不写的话,就是追加模式
                    format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'  # 日志格式
                    )
logging.debug('debug 信息')
logging.info('info 信息')
logging.warning('warning 信息')
logging.error('error 信息')
logging.critical('critial 信息')

If the filename and filemode are set in logging.basicConfig(), only the log will be saved to the file, and will not be output to the console.

3. Input on the previous screen and write log to the file

The logging library has a modular design and provides many components: loggers, processors, filters, and formatters.

  • Logger exposes an interface that can be used directly by application code.
  • The Handler sends the log records (generated by the logger) to the appropriate destination.
  • Filter provides better granular control, it can decide which log records to output.
  • Formatter specifies the layout of the log records in the final output.

Loggers:

The Logger object does three things. First, they expose many methods to the application code so that the application can log messages at runtime. Second, the logger object determines which log messages need to be recorded by severity (the default filtering facility) or filter object. Third, the logger object passes relevant log messages to all interested log processors.

Commonly used methods of logger objects are divided into two categories: configuration and sending messages .

These are the most commonly used configuration methods:

  • Logger.setLevel() specifies the lowest security level log information that the logger will process, debug is the lowest built-in security level, and critical is the highest built-in security level. For example, if the severity is INFO, the logger will only process INFO, WARNING, ERROR and CRITICAL messages, and DEBUG messages will be ignored.
  • Logger.addHandler() and Logger.removeHandler() add and remove handler objects from the logger object. See Handlers for details of the processor.
  • Logger.addFilter() and Logger.removeFilter() add and remove filter objects from the logger object.

Handlers

处理程序The object is responsible for dispatching the appropriate log message (based on the severity of the log message) to the specified target of the handler. Logger Objects can addHandler()add zero or more handler objects through methods. For example, an application can send all log messages to a log file, all log messages of error level and above to standard output, and all critical log messages to a certain email address. In this example, three independent processors are required, each of which is responsible for sending messages of a specific level to a specific location.

There are 4 commonly used: 

1) logging.StreamHandler -> console output 


Use this Handler to output information to any file object similar to sys.stdout or sys.stderr.

Its constructor is:

StreamHandler([strm])

The strm parameter is a file object. The default is sys.stderr


2)    logging.FileHandler -> file output


Similar to StreamHandler, it is used to output log information to a file. But FileHandler will help you open this file. Its constructor is:

FileHandler(filename[,mode])

filename is the file name, you must specify a file name.
mode is how the file is opened. The default is'a', which is added to the end of the file.


3)    logging.handlers.RotatingFileHandler  -> Automatically split the log file according to the size, and regenerate the file once it reaches the specified size 


This Handler is similar to the FileHandler above, but it can manage file size. When the file reaches a certain size, it will automatically rename the current log file, and then create a new log file with the same name to continue output. For example, the log file is chat.log. When chat.log reaches the specified size, RotatingFileHandler automatically renames the file to chat.log.1. However, if chat.log.1 already exists, chat.log.1 will be renamed to chat.log.2. . . Finally, re-create chat.log and continue to output log information. Its constructor is:

RotatingFileHandler( filename[, mode[, maxBytes[, backupCount]]])

The two parameters of filename and mode are the same as FileHandler.
maxBytes is used to specify the maximum file size of the log file. If maxBytes is 0, it means that the log file can be infinitely large, and the renaming process described above will not happen.
backupCount is used to specify the number of backup files to keep. For example, if it is specified as 2, when the renaming process described above occurs, the original chat.log.2 will not be renamed, but will be deleted.


4)    logging.handlers.TimedRotatingFileHandler ->  automatically split log files according to time 


This Handler is similar to RotatingFileHandler. However, it does not determine when to recreate the log file by judging the file size, but automatically creates a new log file at a certain interval. The process of renaming is similar to RotatingFileHandler, but the new file is not appended with a number, but the current time. Its constructor is:

TimedRotatingFileHandler( filename [,when [,interval [,backupCount]]])

The filename parameter and backupCount parameter have the same meaning as RotatingFileHandler.
interval is the time interval.
The when parameter is a string. Represents the unit of the time interval and is not case sensitive. It has the following values:

  • S seconds
  • M points
  • H hours
  • D days
  • W Every week (interval==0 means Monday)
  • midnight every morning
     

Configuration method:

  • setLevel()The method is the same as the log object, and specifies the lowest level of the log that will be distributed. Why are there two setLevel()methods? The level of the logger determines whether the message is passed to the processor. The level of each processor determines whether the message is to be distributed.
  • setFormatter()Choose a formatter for this processor.
  • addFilter()And removeFilter()respectively configure and unconfigure the filter object on the handler.

Formatters

The Formatter object sets the final rules, structure and content of the log information. The default time format is %Y-%m-%d %H:%M:%S. The following are some commonly used information in Formatter

%(name)s

Logger's name

%(levelno)s

Digital log level

%(levelname)s

Log level in text form

%(pathname)s

The full path name of the module that calls the log output function, it may not

%(filename)s

The file name of the module that calls the log output function

%(module)s

The name of the module that calls the log output function

%(funcName)s

The name of the function that calls the log output function

%(lineno)d

The line of code where the statement that calls the log output function is located

%(created)f

The current time, expressed as a floating point number representing the time in the UNIX standard

%(relativeCreated)d

When outputting log information, the number of milliseconds since the Logger was created

%(asctime)s

The current time as a string. The default format is "2003-07-08 16:49:45,896". Milliseconds after the comma

%(thread)d

Thread ID. Maybe not

%(threadName)s

Thread name. Maybe not

%(process)d

Process ID. Maybe not

%(message)s

User output message

 

demand:

Output log to the console and write the log to the log file.
Two types of logs are saved, all.log saves debug, info, warning, critical information, and error.log saves only error information, and the log file is automatically divided according to time.

Sample code:

import logging
from logging import handlers


class Logger(object):
    level_relations = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'crit': logging.CRITICAL
    }  # 日志级别关系映射

    def __init__(self, filename, level='info', when='D', backCount=3, fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
        self.logger = logging.getLogger(filename)
        # 设置日志级别
        self.logger.setLevel(self.level_relations.get(level))
        # 设置日志格式
        format_str = logging.Formatter(fmt)
        # 往屏幕上输出
        sh = logging.StreamHandler()
        # 设置屏幕上显示的格式
        sh.setFormatter(format_str)
        # 往文件里写入指定间隔时间自动生成文件的处理器
        th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount, encoding='utf-8')
        # 实例化TimedRotatingFileHandler
        # interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
            # S 秒
            # M 分
            # H 小时、
            # D 天、
            # W 每星期(interval==0时代表星期一)
            # midnight 每天凌晨
        # 设置文件里写入的格式
        th.setFormatter(format_str)
        # 把对象加到logger里
        self.logger.addHandler(sh)
        self.logger.addHandler(th)


if __name__ == '__main__':
    log = Logger('all.log', level='debug')
    log.logger.debug('debug')
    log.logger.info('info')
    log.logger.warning('警告')
    log.logger.error('报错')
    log.logger.critical('严重')
    Logger('error.log', level='error').logger.error('error')

The results on the screen are as follows:

Since when=D, the time will be added to the newly generated file name [due to time issues, the next generated file will have a time suffix] , as shown below:

Guess you like

Origin blog.csdn.net/weixin_44799217/article/details/114048743