How to log the output from the multiple telnet sessions to separate log file

Chris Angelico rosuav at gmail.com
Sat Oct 17 02:05:48 EDT 2015


On Sat, Oct 17, 2015 at 4:47 PM,  <manjunatha.mahalingappa at gmail.com> wrote:
> class Logger():
>     def __init__(self,log):
>         self.terminal = sys.stdout
>         self.log = log
>
>     def write(self, message):
>         self.terminal.write(message)
>         self.log.write(message)
>
>
>     for (dname, ip, port) in tuples :
>                 sys.stdout =Logger(log)

Every time you construct a Logger, it snapshots the previous value of
sys.stdout. By the time you've set them all up, sys.stdout is the last
Logger created, which will chain to the one created previous to it,
then the one before that, and finally to the actual console.

Do you need to use sys.stdout at all? It would be a lot easier to
ignore stdout and just write your logs directly to files.

ChrisA



More information about the Python-list mailing list