How do I fetch lines in a log file

Matthew

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 ')
Stuart

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')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fetch relative to a JavaScript file

How do I fetch lines before/after the grep result in bash?

How do I comment lines in .plist file?

how do I open file to print lines?

How do I fetch a log of user update changes in Amazon Cognito?

How do you keep only the last n lines of a log file?

which log file or how do I locate this log file?

how can I extract the lines of a big log file in python

How do I filter a log to only the lines between two times?

How do i Empty log file on here?

How do I overwrite a log file in log4j?

How do I upload a file with the JS fetch API?

How do I fetch host and pass from file in linux

How do I fetch name.json file with reactNative locally?

How do I sort a range of lines in a file automatically on save?

how do I format long config file lines

How do I change all of the "/" to ")" in all of lines of a file that end in ")"

How do I split a text file into an array by blank lines?

How do I write all lines from less to a file?

How do I remove certain lines (using line numbers) in a file?

How do I grab the first 10% of lines in a text file?

How do I use the lines of a file as arguments of a command?

Golang: How do I determine the number of lines in a file efficiently?

How do I find recurring pattern lines in a large file

How Do I Ignore Blank Lines Processing a CSV File In Clojure?

How do I remove multiple empty lines in a text file

How do I count number of lines in an imported to array CSV file?

How do I create files using the lines in a .txt file?

How do I iterate over the lines of a (huge) gzipped file?