Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to output and log messages at the same time in python

I want something like what the tee command does in Linux based systems, but in pure python code.

I have tried using this snippet:

import sys

# Define the file name for logging
log_file_name = 'output.log'

# Open the file in append mode
log_file = open(log_file_name, 'a')

# Function to log output to both console and file
def log(message):
    # Write to console
    print(message)
    # Write to file
    log_file.write(message + '\n')
    # Flush the buffer to ensure the message is written immediately
    log_file.flush()

# Redirect stdout and stderr to the log function
sys.stdout.write = log
sys.stderr.write = log


print("Hello world!")

However I get this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

object address  : 0x7f88e6b1c1c0
object refcount : 2
object type     : 0x9e14c0
object type name: RecursionError
object repr     : RecursionError('maximum recursion depth exceeded')
lost sys.stderr

and whenever I comment out the print statement in the log function it logs the message into the log file but does not output the message into the console. Why is this happening and how can I fix it?

>Solution :

Here is one way:

import sys

_print = print

def print(*args, **kwargs):
    _print(*args, **kwargs, file=open('output.log','w'))
    _print(*args, **kwargs)

print("Hello world!")

Explanation

print() function already has an argument for file destination.
https://docs.python.org/3/library/functions.html#print

We preserved the original print function within _print() and call it from our print function to serve our both needs. This way, the calling code doesn’t need to change.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading