Python Realtime filter Log script

Source: Internet
Author: User

Great God Guide works:

#!/usr/bin/python
#coding: UTF8

From Functools Import Wraps
From time import sleep
Import OS

Restart= ' pm2 RESTART ios-push '


# Coroutine First call with Next statement
def coroutine (FN):
@wraps (FN)
def wrapper (*args,**kwargs):
RET=FN (*args,**kwargs)
Next (ret)
return ret
Return wrapper

def follow (File,target):
‘‘‘
Unix-like Tail-f, but accepts a target, has new data, and gives it the target handle.
‘‘‘
File.seek (0,2) # directly to the last line of the file, similar to Tail-f-N 0
While True:
Line=file.readline () #读取行
If not line:
Sleep (0.5) # If the line is empty, go directly to sleep 0.5s, then continue
Continue
Target.send (line) # If you have new data, give it to target processing

def action ():
If Os.system (RESTART) = = 0:
Print (' Ios-push restart success! ')
#print (' restart ')

@coroutine
def grep (pattern):
‘‘‘
grep is a coroutine that takes a pattern parameter and needs to send data from the outside, and if the accepted data matches the pattern passed, then the action function is called
‘‘‘
While True:
Line = yield
If pattern in line:
Action ()
if __name__ = = ' __main__ ':
Follow (open (' Error.log ', ' R '), grep (' Maxlistenersexceededwarn '))


Python Realtime filter Log script

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.