In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to record the program log in Python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Python has a built-in logging module-logging, through which we can easily record simple program logs in Python code.
The logging module divides logs into five levels:
DEBUG: debugging information, usually needed when diagnosing a problem
INFO: general information to confirm that the program is installed and expected to run
WARNING: a warning message indicating that something unexpected has happened, or indicating that some problems may occur next, but the program continues to run
ERROR: error message. Some problems occurred while the program was running, and some functions were not executed.
CRITICAL: danger message, a serious error that prevents the program from running.
The above five levels of log information are implemented using the debug (), info (), warning (), error (), critical () methods of the logging module.
By default, the log level used by logging is warning, which means that only log information at this level and above is recorded, so neither debug information nor info information is displayed by default.
Let's test it:
Run the code, and the console displays:
ERROR:root: an error has occurred
WARNING:root: warning message
Sure enough, the information at the INFO level was not displayed.
We use the basicConfig () method of the logging module to modify a log output level to INFO:
In this way, you can output INFO-level information in the console:
ERROR:root: an error has occurred
INFO:root: printing information
WARNING:root: warning message
In addition to printing the log information to the console, we can also write it to the file. It is also set using the basicConfig () method:
Running the program will generate a text file called test.log, which contains the contents of the log record:
If we run the above code repeatedly, we will find that the log information is appended to the contents of the test.log file:
What if you don't want to do this, also use the filemode parameter in the basicConfig () method to set it:
In this way, the generated log file is a new:
The log information output above shows that all messages are output in a format such as "log level: role: message".
What if we want to change the format of log messages? Also use the basicConfig () method, which is set with its format parameter. Let's take a look at an example:
We set the format to message level and message content. The root message is no longer in the output log:
Logging supports more than these two formats, let's take a look at:
% (asctime) s: normal time when the log was created
% (created) f: time when the log was created (returned by time.time ())
% (filename) s: file name
% (funcName) s: call logging function
% (levelname) s: text level of the log message
% (levelno) s: numeric level of log messages
% (lineno) d: line number of the call log message
% (msecs) d: millisecond portion of creation time
% (message) s: log messages
% (name) s: name of the logger
% (pathname) s: pathname of the source file that logged
% (process) d: process ID
% (processName) s: process name
% (thread) d: thread ID
% (threadName) s: thread name
% (relativeCreated) d: the time the log record was created (in milliseconds)
With these formats, we can customize logging, such as display time:
The above is how to record the program log in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.