【 Abstract 】 Python in logging Detailed explanation of log module !
such as :
Pass below result1 To result5 Simulate the five functions painstakingly written by the partners , As a result, it was wrong when it finally called the ultimate function !
This is how to do —— Also good have print() function , Print one by one to see where the printing is abnormal :
result1 = ' The first function runs OK'
print(result1)
result2 = ' The second function runs OK'
print(result2)
result3 = ' The third function does not run OK'
print(result3)
result4 = ' The fourth function runs OK'
print(result4)
result5 = ' The fifth function runs OK'
print(result5)
Copy code
Undeniable? , This is indeed a solution BUG Methods ! however , When you find BUG After the location and resolution , So many printf() You have to delete them one by one , Is it too much trouble !
We will all the above pirntf() The sentences are all changed to logging.debug() sentence , Observe the output , There is no output at this time —— That is, it has no impact on our procedures at present !
import logging
logging.basicConfig(level=logging.INFO)
result1 = ' The first function runs OK'
logging.debug(result1)
result2 = ' The second function runs OK'
logging.debug(result1)
result3 = ' The third function runs OK'
logging.debug(result1)
result4 = ' The fourth function runs OK'
logging.debug(result1)
result5 = ' The fifth function runs OK'
logging.debug(result1)
Copy code
We set the log level to DEBUG, the level The value of the set logging.DEBUG, Then observe the output :
import logging
logging.basicConfig(level=logging.DEBUG)
result1 = ' The first function runs OK'
logging.debug(result1)
result2 = ' The second function runs OK'
logging.debug(result1)
result3 = ' The third function runs OK'
logging.debug(result1)
result4 = ' The fourth function runs OK'
logging.debug(result1)
result5 = ' The fifth function runs OK'
logging.debug(result1)
Copy code
Will find , At this point, it will be DEBUG Level output information . In this way, we can simply change the log level ( Just change one parameter value ) To control whether to output the display —— To check for errors , Instead of repeatedly adding and deleting print() Function to check for errors . Is it convenient ?
The key! ! The key! !!
Now let's walk into logging The world of modules !!! |
---|
It is a good habit to log the operation of programs in software development , It is very helpful for troubleshooting and system operation and maintenance .Python The standard library comes with a log module , The log function of the program directly calls the log module of the standard library through the log , Developers can clearly understand what happened , Including what went wrong .
Be careful : After specifying the log level , Only log information greater than or equal to the specified log level will be displayed !
The log level (level) | describe |
---|---|
DEBUG | Debugging information , Usually used when diagnosing problems |
INFO | General information , Confirm that the program runs as expected |
WARNING | Warning message , Something unexpected happened , Or indicate that some problems may occur next , But the program continues to run |
ERROR | error message , There are some problems in the running of the program , Some functions of the program cannot be performed |
CRITICAL | Hazard information , A serious mistake , The program cannot continue |
logging Medium level size :DEBUG<INFO<WARNING<ERROR<CRITICAL
import logging
# Format the output
LOG_FORMAT = " Time :%(asctime)s - The log level :%(levelname)s - Log information :%(message)s"
# Yes logger To configure —— The log level & Output format
logging.basicConfig(level=logging.WARNING, format=LOG_FORMAT)
# logging.level(message) Create a level Level of logging
logging.debug("This is a debug log")
logging.info("This is a info log")
logging.warning("This is a warning log")
logging.error("This is a error log")
logging.critical("This is a critical log")
Copy code
Observation can be seen , Indeed, only greater than or equal to WARNING Log level log information is output !
Be careful :logging.basicConfig() There can only be one ! If you write more than one —— Only article 1 will take effect !!!
The final log information used above is output at the terminal —— Once the computer is turned off / Program one off / Close the editor , The log information is lost !
And we won't do that in actual use , So let's see how to write a file !
tip : You can also specify filemode Parameter to specify how to write the file !( Analogy file operation a,a+ etc. )
If it's just a simple use logging, Then use the method described above , If you want deep customization logging, Then we need to have a deeper understanding of it ! |
---|
Components | explain |
---|---|
Loggers( Loggers ) | The interface directly used by the provider ( In the basic operation logging.basicConfig() This component is configured ) |
Handlers( Log processor ) | Send the recorded log to the specified location ( Terminal printing / Save as a file ) |
Filters( Log filter ) | Used to filter specific log records |
Formatters( Log formatter ) | Used to control the output format of log information |
The relationship between the components is shown in the figure below :
import logging
# 1. Create a logger( Loggers ) object ;
my_logger = logging.Logger("first_logger")
# 2. Definition handler( Log processor ), Decide where to send the log ;
my_handler = logging.FileHandler('test.log')
# 3. Set the log level (level) And output format Formatters( Log formatter );
my_handler.setLevel(logging.INFO)
my_format = logging.Formatter(" Time :%(asctime)s Log information :%(message)s Line number :%(lineno)d")
# hold handler Add to the corresponding logger In the middle .
my_handler.setFormatter(my_format)
my_logger.addHandler(my_handler)
# Use :
my_logger.info(" I am the log component ")
Copy code
import logging
# Create a logger( Loggers ) object ;
my_logger = logging.Logger("first_logger")
# The first log processor
my_handler = logging.FileHandler('test.log')
my_handler.setLevel(logging.INFO)
my_format = logging.Formatter(" Time :%(asctime)s Log information :%(message)s Line number :%(lineno)d")
my_handler.setFormatter(my_format)
my_logger.addHandler(my_handler)
# The second log processor
you_handler = logging.StreamHandler()
you_handler.setLevel(logging.DEBUG)
you_format = logging.Formatter(" Time :%(asctime)s Log information :%(message)s Line number :%(lineno)d This is a StreamHandler")
you_handler.setFormatter(you_format)
my_logger.addHandler(you_handler)
# Use :
my_logger.info(" I am the log component ")
Copy code
From now on , Hold the line , Make a little progress every day , Near future , You will thank you for your efforts ! |
---|