log usage

Keywords: Windows Deep Learning Transformer

log usage

It is inevitable that we encounter bugs in the development process. How can we easily find bugs? There is a logging module in Python that can record what is wrong and record relevant information to help us debug easily

log level

Logs are divided into five levels, from low to high:

 1. DEBUG 
 2. INFO 
 3. WARNING 
 4. ERROR 
 5. CRITICAL

explain:

DEBUG: detailed information, which usually only appears in the diagnosis of problems
INFO: confirm that everything runs as expected
WARNING: an indication that something unexpected has happened, or that some problems will occur in the near future (e.g. low disk space "). The software still works as expected.
ERROR: a more serious problem is that the software does not perform some functions
CRITICAL: a serious error, which indicates that the program itself may not continue to run
These five levels also correspond to five logging methods: debug, info, WARNING, error and critical. The default is WARNING, which is tracked only when it is above or above WARNING.

Log output

There are two ways to record the trace, one is to output the console, and the other is to record to a file, such as a log file.
(1) Output console

import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

# Start using the log function
logging.info('This is loggging info message')
logging.debug('This is loggging debug message')
logging.warning('This is loggging a warning message')
logging.error('This is an loggging error message')
logging.critical('This is loggging critical message')

Output results

2021-11-28 11:41:48,157 - exercise1.py[line:19] - INFO: This is loggging info message
2021-11-28 11:41:48,157 - exercise1.py[line:21] - WARNING: This is loggging a warning message
2021-11-28 11:41:48,158 - exercise1.py[line:22] - ERROR: This is an loggging error message
2021-11-28 11:41:48,158 - exercise1.py[line:23] - CRITICAL: This is loggging critical message

Configure the log output format and mode through the logging.basicConfig function. The above code sets the log output level to INFO level, which means that logs above INFO level will be output. In addition, the format of log output is also formulated.
Log format description

The log format is as follows:

In the logging.basicConfig function, you can specify the log output format. This parameter can output many useful information, as follows:
%(levelno)s: print log level value
%(levelname)s: print log level name
%(pathname)s: print the path of the currently executing program, which is actually sys.argv[0]
%(filename)s: print the name of the current executing program
%(funcName)s: current function for printing logs
%(lineno)d: the current line number of the print log
%(actime) s: time to print the log
%(thread)d: print thread ID
%(threadName)s: print thread name
%(process)d: print process ID
%(message)s: print log information

The common formats given in the work are as follows:

 format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

The most common output format:

import logging

logging.basicConfig(level=logging.INFO,
                    format=' %(levelname)s: %(message)s')

# Start using the log function
logging.info('This is loggging info message')
logging.debug('This is loggging debug message')
logging.warning('This is loggging a warning message')
logging.error('This is an loggging error message')
logging.critical('This is loggging critical message')

Output results:

INFO: This is loggging info message
WARNING: This is loggging a warning message
ERROR: This is an loggging error message
CRITICAL: This is loggging critical message

Output log to file

import logging

logging.basicConfig(level=logging.WARNING,
                    filename='./log.txt',
                    filemode='w',
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# use logging
logging.info('This is loggging info message')
logging.debug('This is loggging debug message')
logging.warning('This is loggging a warning message')
logging.error('This is an loggging error message')
logging.critical('This is loggging critical message')

The output will generate a log.txt file. Open the file

2021-11-28 11:47:10,244 - exercise1.py[line:35] - WARNING: ���� loggging a warning message
2021-11-28 11:47:10,244 - exercise1.py[line:36] - ERROR: ���� an loggging error message
2021-11-28 11:47:10,244 - exercise1.py[line:37] - CRITICAL: ���� loggging critical message

Note that as long as the log function is used once, the format setting will fail again. The format will not change frequently in actual development, so the format needs to be set at the beginning

reference material: https://blog.csdn.net/qq_41856814/article/details/90404529

Posted by svivian on Sat, 27 Nov 2021 20:01:35 -0800