Replies: 1 comment
-
I think I know what you mean! See this code maybe it will help you import sys
from rich.console import Console
class MultiWriter:
def __init__(self, *writers):
self.writers = writers
def write(self, message):
for writer in self.writers:
writer.write(message)
def flush(self):
for writer in self.writers:
if hasattr(writer, 'flush') and not getattr(writer, 'closed', False):
try:
writer.flush()
except ValueError:
# Ignore if writer is already closed or cannot be flushed
pass
# Create a file to log the output
log_file = open("output.log", "w")
# Create a console object from Rich
console = Console()
# Replace sys.stdout with an instance of MultiWriter
sys.stdout = MultiWriter(sys.stdout, log_file)
# Now any print statements will go to both the console and the log file
print("This will be printed to both the console and the log file.")
# You can still use the Rich console for enhanced output
console.print("Hello, [bold magenta]Rich[/bold magenta] and log file!")
# Ensure to close the log file properly
log_file.close() If I've misunderstood your qustion, please feel free to let me know! 👍🏻 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey guys, was searching around but couldn't find.
Is is possible to make all console prints to both sys.stdout and a file at same time?
I can see that there is a
file
parameter in the console but it removes output from stdoutBeta Was this translation helpful? Give feedback.
All reactions