Skip to content

Instantly share code, notes, and snippets.

@rjohnsondev
Created January 23, 2013 13:24
Show Gist options
  • Save rjohnsondev/4605551 to your computer and use it in GitHub Desktop.
Save rjohnsondev/4605551 to your computer and use it in GitHub Desktop.
Add coloured labels to logging output :D
import os
import logging
def initLogging(level = logging.DEBUG):
if "color" in os.environ["TERM"]:
logging._levelNames[logging.CRITICAL]= '\033[1m\033[91mCRITICAL\033[0m'
logging._levelNames[logging.ERROR]= '\033[91mERROR\033[0m'
logging._levelNames[logging.WARNING]= '\033[93mWARNING\033[0m'
logging._levelNames[logging.INFO]= '\033[94mINFO\033[0m'
logging._levelNames[logging.DEBUG]= '\033[92mDEBUG\033[0m'
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s: %(message)s"))
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(level)
initLogging()
logging.critical("This is a test")
logging.error("This is a test")
logging.warning("This is a test")
logging.info("This is a test")
logging.debug("This is a test")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment