Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
vm_log_filename = all_vm_details[vm_id][vm_id]['vm_log_filename'][0]
    syslog_record = str(vm_ip) + "_bookmark_log="
    print(syslog_record)
    print("VM Log FIle : ", vm_log_filename)
    
    # Finding the line with index as line number
    try:
        last_line_output = linecache.getline(vm_log_filename, last_line_no)
        print("Content for nth line :", last_line_output)
    except Exception as e:
        print("Exception while reading line from file:", e)
        return None

Code 2-

print("Bookmark Index:", bookmark_index)
        print("Purging Logs...")
        
        try:
            bookmark_line = linecache.getline('/var/log/syslog/LONSTBYRDEV02-10.72.209.183.log', int(bookmark_index))
            print("Bookmark Line :", bookmark_line)
                 
        except Exception as e:
            print("Exception while reading line from file:", e)
            return None
        
        if not bookmark_line:
            print("Bookmark line not found.")
            continue

Code 1 is superbly working fine and getting the line and printing last_line_output and on the other hand , code 2 is not returning anything,

We tried manually hardcoding the index and found that till 800000 we were getting the bookmark line as below-

Bookmark Line : 2023-05-08T00:44:35+00:00 LONSTBYRDEV02 sudo: pam_unix(sudo:session): session closed for user root
This is 800000 th line

and for more than that it was giving below-

Purging Logs...
Bookmark Line :
Bookmark line not found.


The same linecache is working in code1 and code 2 it is showing limitations.