In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of Python environment variables for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Python environment variable has a powerful and flexible industrial recording module, which can log messages to any location at different levels, such as memory, file, network, console and so on. To use this module, you need to make the necessary configuration.
For libraries that want to provide logging, you can choose to let the user use the default configuration of the library (so that it does not disturb the user), or choose to have the user configure the logging feature himself. Of course, if you are a library developer, * * take care of everything for users instead of annoying them. So, what settings should your library configuration itself do?
There are two common options: write the record to a file, or write to the console. But both options can lead to confusion. Until the Python environment variable, the * practice requires library developers to include a small do-nothing handler.
And configure its logger to use this handler. Python 3.1 uses this NullHandler as part of the recording module itself. Let's give an example to illustrate. Suppose your lib.py module contains the library code shown below. There is an init () function that receives the logging handler, but receives the new NullHandler by default.
After that, the logger object is set to use the supplied logger (or the default logger). A logging handler is an object that determines where to output records. The example function a_function_that_uses_logging () here calls the global logger object and logs some interesting messages:
Import HTMLParser import urllib import sys # defines the HTML parser class parseLinks (HTMLParser.HTMLParser): def handle_starttag (self, tag, attrs): if tag = = 'a': for name Value in attrs: if name = = 'href': print value print self.get_starttag_text () # create an instance of the HTML parser lParser = parseLinks () # Open the HTML file lParser.feed (urllib.urlopen (\ "http://www.python.org/index.html").read()) lParser.close ())
To do this, our code configures this handler to save only 250 bytes per log file and to maintain a maximum of 5 backup files. * *, the code calls the a_function_that_uses_logging () function. The following application code is configured with a roundabout file handler, which is an improved handler for long-running systems that generate a large amount of log information.
This handler limits the amount of logging information in each file and maintains a preset number of backup files. These restrictions ensure that the log file does not exceed the specified size and always retain the logging information (before this limit is reached).
This is good because sometimes users don't care about logged messages-what they want is that they don't need to configure logger when calling functions, and these logs don't run out of hard disk space and don't have a full screen of messages. At this point, NullHandler can come in handy.
The following code does the same thing as above, but it does not configure a logging handler, so it does not get logging results. Note that logging and logging.handlers are not imported here, and it is not determined which handler to use and how to describe the Python environment variable.
This is the end of this article on "sample analysis of Python environment variables". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.