Created
May 18, 2018 17:43
-
-
Save antonjlin/5fd9d7d48d224a362967deabdfa42eec to your computer and use it in GitHub Desktop.
An incredibly useful python logger.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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