Python + logging output to the screen, write log log to file (pro test)

Log

Logs are a method of tracking 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 may optionally contain variable data (ie, potentially different data for each occurrence of the event). Incidents also have the importance developers attribute to incidents; importance can also be referred to as 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 Detailed information is generally only used when debugging problems.
INFO Prove that things are working as expected.
WARNING Prompt for some unexpected events or problems that may appear 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 this level and above will feedback information unless the logging module is used to do other things.

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

 

1. Print to the console

import logging
logging.debug('debug 信息')
logging.warning ('Only this will output ...')
logging.info ('info information')

Since the default level is warning, all warning-only information will be output to the console.

WARNING: root: Only this will output. . .

 

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

Copy code

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 information')
logging.warning('warning 信息')
logging.error ('error information')
logging.critical ('critial information')

Copy code

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

Log level: debug <info <warning <error <critical
logging.debug ('debug level, the lowest level, general developers use to print some debugging information')
logging.info ('info level, normal output information, generally used to print some normal operations')
logging.warning ('waring level, generally used to print warning information')
logging.error ('error level, generally used to print some error messages')
logging.critical ('critical level, generally used to print some fatal error messages, the highest level')

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

Copy code

logging.basicConfig (level = logging.DEBUG, # Log level printed by the console
                    filename='new.log',
                    filemode = 'a', ## mode, there are w and a, w is write mode, the log will be rewritten every time, overwriting the previous log
                    #a is the append mode, the default is append mode if it is not written
                    format=
                    '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                    #Log format
                    )

Copy code

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

 

Third, the previous screen input, but also 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 application code can directly use.
  • The Handler sends the log records (generated by the recorder) to the appropriate destination.
  • Filter provides better granularity control, it can decide which log records to output.
  • Formatter indicates the layout of the log records in the final output.

Loggers:

Logger objects do three things. First, they expose many methods to the application code so that the application can log messages at runtime. Secondly, 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 related log messages to all interested log processors.

Common methods of logger objects fall 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 recorder will only process INFO, WARNING, ERROR, and CRITICAL messages, and DEBUG messages are ignored.
Logger.addHandler () and Logger.removeHandler () add and remove handler objects from the logger object. See Handlers for details.
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 designated 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 log messages of critical level (critical) to an 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])
where 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 the way 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 the 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, it will first rename chat.log.1 to chat.log.2. . . Finally, recreate chat.log and continue to output log information. Its constructor is:
RotatingFileHandler (filename [, mode [, maxBytes [, backupCount]]])
where 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, which means that the log file can be infinitely large, the renaming process described above will not occur.
backupCount is used to specify the number of reserved backup files. For example, if 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, but it does not determine when to re-create the log file by judging the file size, but automatically creates a new log file at a certain interval. The renaming process is similar to RotatingFileHandler, but the new file is not appended with numbers, but the current time. Its constructor is:
TimedRotatingFileHandler (filename [, when [, interval [, backupCount]]])
where the filename parameter and backupCount parameter have the same meaning as RotatingFileHandler.
interval is the time interval.
when parameter is a string. The unit for the time interval is not case sensitive. It has the following values:
S seconds
M minutes
H hours
D days
W every week (interval == 0 represents Monday)
midnight every morning
 

Configuration method:

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

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 of Formatter

%(name)s

Logger's name

%(levelno)s

Log level in numbers

%(levelname)s

Log level in text

%(pathname)s

The full path name of the module calling the log output function, may not be

%(filename)s

File name of the module calling the log output function

%(module)s

The name of the module calling the log output function

%(funcName)s

Function name 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 time in the UNIX standard

%(relativeCreated)d

The number of milliseconds since the creation of the Logger when outputting log information

%(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

The thread name. Maybe not

%(process)d

Process ID. Maybe not

%(message)s

User output message

 

 

demand:

Output log to console and write log to log file.
Save 2 types of logs, all.log saves debug, info, warning, critical information, error.log only saves error information, and automatically divides the log file according to time.

Copy 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
    } #Log level relationship mapping

    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)
        format_str = logging.Formatter (fmt) #Set the log format
        self.logger.setLevel (self.level_relations.get (level)) # Set the log level
        sh = logging.StreamHandler () # output to the screen
        sh.setFormatter (format_str) #Set the format displayed on the screen
        th = handlers.TimedRotatingFileHandler (filename = filename, when = when, backupCount = backCount, encoding = 'utf-8') # Write into the file # The processor that automatically generates the file at the specified interval
        #Instantiate TimedRotatingFileHandler
        #interval is the time interval, backupCount is the number of backup files, if it exceeds this number, it will be automatically deleted, when is the time unit of the interval, the unit has the following types:
        # S seconds
        # M points
        # H hours,
        # D days,
        # W every week (interval == 0 represents Monday)
        # midnight Every morning
        th.setFormatter (format_str) #Set the format written in the file
        self.logger.addHandler (sh) #Add object to logger
        self.logger.addHandler(th)
if __name__ == '__main__':
    log = Logger('all.log',level='debug')
    log.logger.debug('debug')
    log.logger.info('info')
    log.logger.warning ('Warning')
    log.logger.error ('Report error')
    log.logger.critical ('critical')
    Logger('error.log', level='error').logger.error('error')

Copy code

The results on the screen are as follows:

2018-03-13 21:06:46,092 - D:/write_to_log.py[line:25] - DEBUG: debug
2018-03-13 21:06:46,092 - D:/write_to_log.py[line:26] - INFO: info
2018-03-13 21:06:46,092 - D:/write_to_log.py[line:27] - WARNING: 警告
2018-03-13 21:06:46,099 - D:/write_to_log.py[line:28] - ERROR: 报错
2018-03-13 21:06:46,099 - D:/write_to_log.py[line:29] - CRITICAL: 严重
2018-03-13 21:06:46,100 - D:/write_to_log.py[line:30] - ERROR: error

Since when = D, the newly generated file name will have time, as shown below.

Published 44 original articles · 130 praises · 1.37 million views

Guess you like

Origin blog.csdn.net/gb4215287/article/details/105336418