Python standard library - logging module

The Logging module can replace the function of the print function, and can save the standard output input to the log file, and the use of the logging module can partially replace the debug function and debug the program.

1. Several levels of the logging module. By default the logging module has 6 levels. The NOSET value is 0, the DeBUG value is 10, the INFO value is 20, the WARNING value is 30, the ERROR value is 40, and the CRITICAL value is 50 (you can also define it yourself).

2. The purpose of these levels is to first set a level for your own logs. The information level sent by the logging module is higher than the defined level and will be displayed on the standard output (screen). Messages sent at a lower level than the defined level are ignored.

If no level is defined, the default defined level is WARNING

testLogging.py is used to test the logging module and define its log level as INFO

import logging 
class TestLogging(object):
def __init__(self):
logFormat = '%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s'
logFileName = './testLog.txt ' #The

script uses different levels to send several pieces of information to the logger, the debug level is lower than info, no output, and all others are output
logging.basicConfig(level=logging.INFO,
format=logFormat,
filename=logFileName,
filemode='w')

logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

if __name__ == "__main__":
test = TestLogging()
The results printed in testLog after running are as follows:

3. Write testLogging.py. Where you need to add logs, you can call logging to locate the problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325196175&siteId=291194637