Skip to content

Instantly share code, notes, and snippets.

@randomradio
Created July 20, 2016 22:43
Show Gist options
  • Save randomradio/d2017117fb4f7c43c344ccc36a8ac58f to your computer and use it in GitHub Desktop.
Save randomradio/d2017117fb4f7c43c344ccc36a8ac58f to your computer and use it in GitHub Desktop.
minimal python logger
import sys
class Logger(object):
"""
this object will repalce default stdout object.
logging info into terminal with sys.stdout
save logging info into log file
{TODO} accept log file as argument
"""
def __init__(self):
self.terminal = sys.stdout
self.log = open("orders_log", "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
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
sys.stdout = Logger()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment