Python logs require constant learning.

Source: Internet
Author: User

Python logs require continuous learning in the continuous development. Only by constantly learning can you better master the relevant usage methods. Next we will introduce in detail how to write the relevant code. Hope to help you.

Classes implement a simple template mode, defining setup, handle, and finish to reload successors, mode Method _ init _ defines the call sequence of the three methods to ensure the operation of the three methods at the same time. Obviously, if we want to close the connection at exit, it is natural to redefine finish.

 
 
  1. def finish(self):  
  2. self.request.close() 

The second question is how to log. Python has the logging module logging.

 
 
  1. import logging  
  2. logging.basicConfig(level=logging.DEBUG,  
  3. format='%(asctime)s %(levelname)s %(message)s',  
  4. filename='log.txt',  
  5. filemode='a+') 

However, we need to make a little supplement in actual use. In multi-threaded programs, to record logs, you need a unique ID related to the thread to identify something. I have not found the direct thread ID. Which brother has found it?) But Python has a built-in function named id to return the identity of an object. Note 1 ). We can get a beautiful output by predefining a template for the information to be recorded.

 
 
  1. def LogTemplate(self, s):  
  2. return '[id.' + str(id(self.request)) + ']: ' + str(s)def Log(self, s):  
  3. ss = self.LogTemplate(s)  
  4. print ss  
  5. logging.info(ss)  
  6. def LogErr(self, s):  
  7. ss = self.LogTemplate(s)  
  8. print ss  
  9. logging.error(ss) 

Now we can write it like this.

 
 
  1. Def setup (self ):
  2. Self. Log ('enter processing thread ')
  3. Def finish (self ):
  4. Self. request. close ()
  5. Self. Log ("Exit Processing thread ")

In addition, the binascii module is also useful for Python logs. I will use binascii. b2a_hex is used to convert a string of binary data into visible ASCII data. It is better to use b2a_hex to convert the received data before logging.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.