Skip to content

Instantly share code, notes, and snippets.

@antonjlin
Created May 18, 2018 17:43
Show Gist options
  • Save antonjlin/5fd9d7d48d224a362967deabdfa42eec to your computer and use it in GitHub Desktop.
Save antonjlin/5fd9d7d48d224a362967deabdfa42eec to your computer and use it in GitHub Desktop.
An incredibly useful python logger.
from datetime import datetime
from termcolor import cprint, colored
import colorama
import sys
colorama.init()
def log(text,color=None,taskNum=None,timestamp=True,overWrite=False):
if overWrite:
sys.stdout.write("\x1b[1A") # Cursor up one line
sys.stdout.write("\x1b[2K") # ERASE
toPrint = ""
if taskNum != None:
task = "[" + str(taskNum) + "] "
else:
task = ""
if timestamp:
timestamp = "[" + str(datetime.now().strftime("%H:%M:%S.%f")[:-4]) + "] "
else:
timestamp = ""
total = "{}{}{}".format(task,timestamp,text)
if color !=None:
toPrint = colored(total, color)
else:
toPrint = total
print(toPrint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment