Hướng dẫn python tail file continuously - tệp đuôi trăn liên tục

Tôi đã triển khai Python Tail -f với đoạn mã sau đây hoạt động hoàn toàn tốt khi chương trình của tôi chạy liên tục trong nền bởi Python MyProgram.py &python myprogram.py &

def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

Tệp được chuyển đến hàm trên là một tệp nhật ký và được truyền từ chính

    # follow.py

    # Follow a file like tail -f.

import smtplib
import time
import re
import logging

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from job import Job


def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

def sendMail(job,occurtime):
    COMMASPACE = ', '
    outer =  MIMEMultipart()
    # msg = MIMEMultipart('alternative')
    outer['Subject'] = 'ETL Failed for job:' + job
    outer['From'] = ''
    me=  ''
    family = [""]
    outer['To'] = COMMASPACE.join(family)

    inner = MIMEMultipart('alternative')
    html = """\
        
          
          
            

Dears,
Please take necessary action to troubleshoot the ETL Error for job:""" + job + " at " + occurtime + """

""" # Record the MIME types of both parts - text/plain and text/html. part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. inner.attach(part2) outer.attach(inner) # Connect to SMTP server and send the email # Parameter are from=me, to=family and outer object as string for message body try: s = smtplib.SMTP('localhost') s.sendmail(me,family,outer.as_string()) s.quit() except SMTPException: logging.info('Unable to send email') if __name__ == '__main__': while True: logging.basicConfig(filename='/opt/etlmonitor/monitor.log',format='%(asctime)s %(levelname)s %(message)s',level=logging.DEBUG, filemode='w') # Define two ETL Job object to store the state of email sent as boolean flag fm =Job() ncell =Job() try: with open("/opt/report/logs/GraphLog.log","r") as logfile: # Continually read the log files line by line loglines = follow(logfile) # Do something with the line for line in loglines: # Extract the last word in the line of log file # We are particulary looking for SUCCESS or FAILED word # Warning!! leading whitespace character is also matched etlmsg= re.search(".*(\s(\w+)$)",line) if etlmsg: # Remove leading whitespace foundmsg = etlmsg.group(1).lstrip() # Process on the basis of last word # If it is SUCCESS , set the job mailsent flag to False so that no email is sent # If it is FAILED and mailsent flag of job is False, send a email and set mailsent flag to True # If it is FAILED and mailsent flag of job is True, do nothing as email was already sent if foundmsg=='SUCCESS': jobname= re.search(": Graph '(.+?)\'",line) if jobname: foundjob= jobname.group(1) if foundjob =='Mirror.kjb': logging.info('Ncell Mirror job detected SUCCESS') ncell.p == False elif foundjob =='FM_job.kjb': fm.p == False logging.info('Ncell Report job detected SUCCESS') else: logging.info('No job name defined for success message') elif foundmsg =='FAILED': jobname= re.search(": Graph '(.+?)\'",line) timevalue=re.search("(.+?)\,",line) if jobname and timevalue: foundjob= jobname.group(1) foundtime = timevalue.group(1) if foundjob =='Mirror.kjb': if ncell.p == True: logging.info('Notification Email has been already sent for job: ' + foundjob) elif ncell.p == False : ncell.p = True sendMail(foundjob,foundtime) else: logging.info("state not defined") elif foundjob =="FM_job.kjb": if fm.p == True: logging.info('Notification Email has been already sent for job: ' + foundjob) elif fm.p == False: fm.p = True sendMail(foundjob,foundtime) else: logging.info('Unkown state of job') else: logging.info('New job name found') except IOError: logging.info('Log file could not be found or opened')

Những gì tôi thực sự đang làm với dòng là đọc từ cuối cùng trong dòng với biểu thức thông thường và thực hiện một số nhiệm vụ dựa trên từ cuối cùng được nhận.

Vấn đề là, tệp nhật ký (graphlog.log) đang được cuộn dựa trên kích thước tệp.Khi điều này xảy ra, chương trình của tôi cũng dừng lại.Làm cách nào để tôi liên tục đọc tệp graphlog.log mà không có chương trình của tôi bị chấm dứt (không có một số lỗi) ngay cả sau khi tệp nhật ký được cuộn theo kích thước tệp và ngày.

Bất kỳ sự giúp đỡ nào cũng được đánh giá cao.