Home > OS >  How do I fetch lines in a log file
How do I fetch lines in a log file

Time:08-02

The program I wrote prints the data in the log file to the GUI. It performs filtering operations by selecting some features. I'm fetching data line by line I can fetch the line with the words according to the date and time format.

But what I want is to fetch the lines above the lines I want. When I type 5 in the Entry, I want the word I want to search to be taken 5 lines above.

For example, my word is 'Timer'. When I write Timer in the entry and I select the before lines checkbox and write 5 in the before line entry. I want to take this;

[01/01/70 02:00:18.699984 ] [debug  ] [1403] [DmTr069EventHandler.c:55] [dmTr069EventHandler_init] LEAVED 
[01/01/70 02:00:18.700122 ] [debug  ] [1403] [DmUkaEventHandler.c:50] [dmUkaEventHandler_init] ENTERED 
[01/01/70 02:00:18.700143 ] [debug  ] [1403] [DmUkaEventHandler.c:52] [dmUkaEventHandler_init] LEAVED 
[01/01/70 02:00:18.700154 ] [debug  ] [1403] [DmAppEventHandler.c:81] [dmAppEventHandler_init] ENTERED 
[01/01/70 02:00:18.700237 ] [debug  ] [1403] [Timer.c:441] [addTimerToSortedTimerList] ENTERED 

The code is here. I tried something but it didn't work for beforeline feature.

def search(msg, startingDate, endingDate, beforeLine, varBefore):
# clear current result
text.delete('1.0', 'end')
with open('OAM.log', 'r', encoding='latin1') as fp:
    global l_no
    for l_no, line in enumerate(fp, 1):
        if msg and msg not in line:
            # does not contain search message, skip it
            continue
        if startingDate or endingDate:
            # get the timestamp
            timestamp = parse_date(line[1:25])
            # within startingDate and endingDate ?
            if startingDate and timestamp < startingDate:
                # before given starting date, skip it
                continue
            if endingDate and timestamp > endingDate:
                # after given ending date, skip it
                continue

        """for count, beforeLine in enumerate(fp, 1):
            #bfline = fp.readlines(l_no - count)
            count -= 1
            text.insert('end', ' \n ')
            text.insert('end', f'Before Line Number: {l_no - beforeEntryVar.get()} Log: {beforeLine}')
            text.insert('end', ' \n ')"""
            
        # insert the log
        text.insert('end', ' \n ')
        text.insert('end', f'Line Number: {l_no} Log: {line}')
        text.insert('end', ' \n ')

CodePudding user response:

The easiest way to do this is with a deque as follows:

import re
from collections import deque

def read_logfile(filename, key, maxlines):
    p = r'(?<=\[). ?(?=\])'
    d = deque([], maxlines)
    with open(filename) as log:
        for line in map(str.strip, log):
            d.append(line)
            if len(m := re.findall(p, line)) > 3 and m[3].startswith(key):
                return d
    return []

print(*read_logfile('OAM.log', 'Timer', 5), sep='\n')
  • Related