In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Perhaps we often use print to output information to windows, but when we have a lot of py files to run, the project is better than
When the time comes, the print is simply too low. Then we can use the powerful logging module to
Output to a file in a path of the specified local pc.
I. The framework of logging
Loggers: An interface that can be called directly by the program. The app records logs by calling the API provided.
Handlers: Decide to assign log records to the correct destination
3. Filters: filters log information and provides judgment on whether to output more fine-grained logs
4. Formatters: Formulate the format layout of the final record print
II. Log level
The system has 6 levels by default. Priority:
CRITICAL 50ERROR 40WARNING 30INFO 20DEBUG 10NOTSET 0
When setting the log to print, you only need to set the priority, such as setting print INFO, then WARNING/ERROR/CRITICAL with higher priority than INFO will be printed.
3. Normal printing
Only ERROR and WARNING messages are output here, because the default output level of logging is WARNING.
4. Output to specified file
Let's look at code written like this:
import loggingimport unittestclass lgtest(unittest.TestCase): logging.basicConfig(filename='../ LOG/'+__name__+'.log',format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level = logging.DEBUG,filemode='a',datefmt='%Y-%m-%d %I:%M:%S %p') def test(self): logging. error ("This is a print of an error message ") logging.info logging. warning ("This is a print of a warning message ") logging. debug ("This is a debug message print ") if__name__=='__main__': unittest.main()
Use logging. basicConfig to specify the file to output and the log output format, including time format, log level, and so on.
Filename: Specify the file path. Here +-name-+ is the filename that names log as the current py
Format: Sets the display format of logs (i.e., the format seen in documents). Time + current file name + log output level + output information
Level: log level of output, priority lower than the set level will not be saved to log document output
Filemode: log open mode
A: It means that the program continues to log every time it runs. That is, it does not overwrite previously saved log information.
W: Rewrite the log every time you run the program. That is, overwrite previously saved log information
V. Final log file
The file name is the same as py because basicConfig uses name to get it.
Let's look at the contents, which are the contents of the py file that has been run twice:
[2018 - 02 - 10 02:29:57 PM-Logout. py-ERROR: This is an error message print][2018 - 02 - 10 02:29:57 PM-Logout. py-INFO: This is a print of info message][2018 - 02 - 10 02:29:57 PM-Logout. py-WARNING: This is a print of warn message][2018 - 02 - 10 02:29:57 PM-Logout. py-WARNING: This is a print of warn message][2018 - 02 - 10 02:29:57 PM-Logout. py-WARNING: This is a print of warn message] 57 PM-Logout. py-DEBUG: This is a debug message print][2018 - 02 - 10 02:39:32 PM-Logout. py-ERROR: This is an error message print][2018 - 02 - 10 02:32 PM] 39:32 PM-Logout. py-INFO: This is a print of info][2018 - 02 - 10 02:39:32 PM-Logout. py-WARNING:[2018 - 02 - 10 02:39:32 PM-Logout. py-DEBUG: This is a debug message print] 4 messages will be printed once run, because filemode is set to a, so it will not be overwritten when running again.
There were eight pieces of information in the previous log.
Ok, log output to documents is that simple. Of course, if you get familiar with the can write their own recorders, filters, etc.
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.