python即時過濾日誌指令碼

來源:互聯網
上載者:User

標籤:python

大神指導作品:

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

from functools import wraps
from time import sleep
import os

RESTART=‘pm2 restart ios-push‘


# coroutine 先要用 next 語句調用一次
def coroutine(fn):
    @wraps(fn)
    def wrapper(*args,**kwargs):
        ret=fn(*args,**kwargs)
        next(ret)
        return ret
    return wrapper

def follow(file,target):
    ‘‘‘
    類似 Unix 的 tail -f, 但是接受一個 target, 有新的資料,交給 target 處理
    ‘‘‘
    file.seek(0,2)# 直接到檔案的最後一行,類似 tail -f -n 0
    while True:
        line=file.readline()#讀取行
        if not line:
            sleep(0.5)# 如果是空行,直接 sleep 0.5s,然後 continue
            continue
        target.send(line)# 如果有新資料,則交給 target 處理

def action():
   if os.system(RESTART) == 0:
       print(‘ios-push restart success!‘)
    #print(‘restart‘)

@coroutine
def grep(pattern):
     ‘‘‘
    grep 是一個 coroutine,接受一個 pattern 參數,需要從外部 send 資料 ,如果接受的資料匹配 pattern 通過了,則調用action函數
    ‘‘‘
    while True:
        line = yield
        if pattern in line:
            action()
if __name__ == ‘__main__‘:
    follow(open(‘error.log‘,‘r‘),grep(‘MaxListenersExceededWarn‘))


python即時過濾日誌指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.