siamii
siamii

Reputation: 24114

Prepend entry to log file

I'm using the Python logging module. Each time I open the log file I have to scroll all the way down to read the latest entries. Is there a way I can make the logger prepend entries to the beginning of file?

Upvotes: 4

Views: 1695

Answers (4)

David Alyakobi
David Alyakobi

Reputation: 1

I created a three step process to solve this issue.

Step 1: Preserve old logs by renaming the old log file:

def Preserve_old_logs():
   os.rename('log.txt', 'previous-log.txt')

step 2: Log your new info into a file using 'a+' read mode:

def Log(text_to_append):
"""Append given text as a new line at the end of file"""
# Open the file in append & read mode ('a+')
with open('log.txt', "a+") as file_object:   
     # Move read cursor to the start of file.
    file_object.seek(0)
    # If file is not empty then append '\n'
    data = file_object.read(100)
    if len(data) > 0:
        file_object.write("\n")
    # Append text at the end of file
    file_object.write(text_to_append)

Step 3: At the end of your program append previous logs to current log. I like to do this with a try, catch, finally where finally will run step 3 and the try phase will run step 1 before the program starts.

def Append_previous_logs():
    with open('log.txt', 'a+') as file_object:
        with open('previous-log.txt', 'r') as old_log:
            file_object.write(old_log.read())
    os.remove('previous-log.txt')

Upvotes: 0

Hai Vu
Hai Vu

Reputation: 40763

I concur with others: prepending to a file is costly, and making your code more complex than it should be. If you view your log using tools such as more or less, then press the capital G key will bring you all the way to the end.

Upvotes: 1

synthesizerpatel
synthesizerpatel

Reputation: 28036

Prepending data to the head of a log file would be quite resource intensive because you'd have to write out the entire file every time.

Instead of 'scrolling' at all, why not just use 'tail -f ' to seek to the end and begin reading? On Linux you can use tail -F , which will re-open the logfile if it gets removed/recreated on the fly.

Alternatively, you could create a new log file for every time you start the program using a timestamp.

Or, you could log to stderr and run your program interactively.

Or you could log to syslog.

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174662

This is not possible through the logger. If you want this, you'll have to write your own custom logger, read all the entries in the log file, add the new entry to the top, then write the entire log file again.

Upvotes: 2

Related Questions