I need to save to file same logs as it shows in console so I edit sys.stdout. (Using code from Redirect stdout to a file in Python?)
But the problem shows when i tried to edit text in write function by adding something before it. In result this "[stack]" adds before and after the text variable.
import sys
class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.file = open("log.txt", "a")
    def flush(self):
        self.terminal.flush()
        self.file.flush()
    def write(self, text):
        self.terminal.write("[stack]" + text)
        self.file.write(text)
        self.flush();
sys.stdout = Logger()
print "Test log"
print "Another test log"
Result:
[stack]Test log[stack]
[stack]Another test log[stack]
 
     
     
    