SoFunction
Updated on 2024-10-29

A python decorator implementation for automatic monitoring of exception code occurrences

Abnormal, should not exist, but we sometimes encounter such a situation, for example, when we monitor the server, every second to collect information, so one second did not collect the information we want, but the next second to collect, and then every time the collection can be collected, just that one time can not be collected, we should analyze the failure of the collection of this one time, this kind of situation can not be repeated, we can not avoid because there are too many external factors, so we will have such a need, how many times in a period of time to display an alarm, or that a period of time, we can not control these external factors. This kind of situation can not be repeated, we can not avoid, because there are too many external factors, we can not control these external factors, so we will have such a need, a period of time how many times the frequency of appearance, we can show an alarm, or, a period of time the frequency of appearance reaches our abnormal license range we believe that this is anomalous, we can send out an alarm.

So how do we implement it, I think of decorator, when the program execution to the exception, I record the time, write to a file, and then read the most recent fifth judgment, the difference between the two timestamp's is only less than 60s, I think that such can be sent to send an alert, if it is greater than 60s, it is considered insufficient to send our warning, the

So let's look at how our code should be written.

import datetime,time,random
def make(func):
  def mak(*args,**kwargs):
    try:
      func(*args,**kwargs)
    except:
      with open('','a+') as f:
        except_time=()
        (except_time.strftime('%Y-%m-%d %H:%M:%S')+'\n')
        ()
      with open('','rb') as m:
        try:
          date=()[-5].decode('utf-8')
          ne=(('\r\n')[0])
          f1=(ne,'%Y-%m-%d %H:%M:%S')
          if (except_time-f1).seconds<6:
            print('Exception! !fail')
          else:
            print('Normal!')
          ()
        except:
          print('Crossing the border means our experiment was a success')
  return mak
@make
def beijing(i,m):
  print(i/m)
if __name__=="__main__":
  while True:
    f=([0,1,2,3])
    n=([0,1,2,3])
    beijing(f,n)
    (0.3)

This way, we can monitor an exception for a program. Let's run the code for this exception monitoring. The

We can see that our code works fine, so let's try it, we monitor the code of multiple programs, our script can not be achieved.

@make
def shanghai(i,m):
  print(i/m)
@make
def rizhao(i,m):
  print(i/m)
@make
def zhengzhou(i,m):
  print(i/m)

PS: Got a question no one can answer? Need Python learning materials? Click the link below to get it yourself!
/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76

We're going to add these methods, and we're going to run them.

We can see that whenever there is an exception, our program will record it, but of course, such can not be properly utilized to our work.

Later, you can combine the practice of the exception monitoring part here with the exception monitoring script I wrote earlier.

summarize

To this point this article on python decorator to achieve the exception code appears to automatically monitor the implementation of the method of implementation of the article is introduced to this, more related python decorator automatically monitor the content of the search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!