Giter Club home page Giter Club logo

Comments (3)

JieyuZ2 avatar JieyuZ2 commented on May 22, 2024 1

My solution is

class Transcript(object):

    def __init__(self, filename):
        self.terminal = sys.stdout
        self.logfile = open(filename, "w")

    def write(self, message):
        self.terminal.write(message)
        self.terminal.flush()
        self.logfile.write(message)
        self.logfile.flush()

    def flush(self):
        # this flush method is needed for python 3 compatibility.
        # this handles the flush command by doing nothing.
        # you might want to specify some extra behavior here.
        pass


def start(filename):
    """Start transcript, appending print output to given filename"""
    sys.stdout = Transcript(filename)


def stop():
    """Stop transcript and return print functionality to normal"""
    sys.stdout.logfile.close()
    sys.stdout = sys.stdout.terminal


start('filename')

main()

stop()

from autogen.

JieyuZ2 avatar JieyuZ2 commented on May 22, 2024

speaking of this, could add a feature to also save all print to a file at the same time?

from autogen.

NeoRyang avatar NeoRyang commented on May 22, 2024

`
import sys
import logging

class LoggerWriter:
def init(self, level):
self.logger = logging.getLogger()
self.level = level
self.message = ''
def write(self, message):
if '\n' in message:
self.message += message.rstrip('\n')
if self.message:
self.logger.log(self.level, self.message)
self.message = ''
else:
self.message += message
def flush(self):
pass
def isatty(self):
return False

console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(message)s'))

file_handler = logging.FileHandler('myapp.log', 'w')
file_handler.setFormatter(logging.Formatter('%(message)s'))

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(console_handler)
logger.addHandler(file_handler)

fmt = 'Thinking .... - %(message)s'
formatter = logging.Formatter(fmt)
file_handler.setFormatter(formatter)

sys.stdout = LoggerWriter(logging.INFO)
sys.stderr = LoggerWriter(logging.ERROR)

if name == 'main':
print("This will go", end=" ")
print("to the log file")
`

from autogen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.