Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.9 kB
2
Indexable
Never
Code block-

try:
        with open(vm_log_filename, 'r') as input_file, open(filename, 'w') as output_file:
            # Using itertools.islice to copy lines between start and end indexes
            output_file.writelines(itertools.islice(input_file, X - 1, Y))

        #Counting the number of lines in file
        with open(filename,'r') as output_file:
            line_count=len(output_file.readlines())
        print("Number of lines in the output file:", line_count)
        logging.info("Number of lines in the output file %d ",line_count)
    except Exception as e:
        print("Exception while copying lines to file:", e)
        logging.error("Exception while copying lines to file: %s",e)
        return None

    # Updating the last index in a Bookmark File
    print("Updating the last index in a Bookmark File")
    bookmark_record = syslog_record
    print(f"Bookmark Record for {i} is {bookmark_record}")
    print(f"Updating Bookmark record for {bookmark_record}")
    updated_bookmark_index = int(old_line_number) + int(line_count)
    updated_bookmark_rec = f"{bookmark_record}{updated_bookmark_rec}\n"
    print(updated_bookmark_rec)
    logging.info("Updated Bookmark Index %d ",updated_bookmark_rec)

Output/Error-

Bookmark Record 172.19.5.134_bookmark_log=1

Old line number from bookmark file is :  1

Value of New Line Number is : 10001
172.19.5.134_bookmark_log=
VM Log FIle :  /syslog/anewavqasapweb01-172.19.5.134.log
Content for nth line : 2023-08-20T02:35:45+00:00 anewavqasapweb01 zypper_log: 2023-08-20 02:35:36 <1> anewavqasapweb01(18993) [zypp++] RepoInfo.cc(probeCache):64 Probed cached type NONE at

172.19.5.134-T2023-08-21-11-30-02.txt
New Value is 10001
Number of lines in the output file: 10001
Updating the last index in a Bookmark File
Bookmark Record for 245 is 172.19.5.134_bookmark_log=
Updating Bookmark record for 172.19.5.134_bookmark_log=
Error in perform_functions_with_bookmark_record function: local variable 'updated_bookmark_rec' referenced before assignment
--- Logging error ---
Traceback (most recent call last):
  File "Dataflow_syslog_postgre.py", line 227, in perform_functions_with_bookmark_record
    copying_loglines_and_updating_bookmark(bookmark_record, syslog_record_no, i, number_of_lines)
  File "Dataflow_syslog_postgre.py", line 292, in copying_loglines_and_updating_bookmark
    updated_bookmark_rec = f"{bookmark_record}{updated_bookmark_rec}\n"
UnboundLocalError: local variable 'updated_bookmark_rec' referenced before assignment

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/logging/__init__.py", line 994, in emit
    msg = self.format(record)
  File "/usr/lib64/python3.6/logging/__init__.py", line 840, in format
    return fmt.format(record)
  File "/usr/lib64/python3.6/logging/__init__.py", line 577, in format
    record.message = record.getMessage()
  File "/usr/lib64/python3.6/logging/__init__.py", line 338, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "Dataflow_syslog_postgre.py", line 362, in <module>
    check_log_enable()
  File "Dataflow_syslog_postgre.py", line 163, in check_log_enable
    check_bookmark_record(vm_id)
  File "Dataflow_syslog_postgre.py", line 190, in check_bookmark_record
    perform_functions_with_bookmark_record(bookmark_line, i)
  File "Dataflow_syslog_postgre.py", line 230, in perform_functions_with_bookmark_record
    logging.error("Error in perform_functions_with_bookmark_record function:", e)
Message: 'Error in perform_functions_with_bookmark_record function:'
Arguments: (UnboundLocalError("local variable 'updated_bookmark_rec' referenced before assignment",),)


2 problems here-
1. we are not getting print("Number of lines in the output file:", line_count) here the line count even on adding print statement.
2. Logging error