Created
January 23, 2013 13:24
-
-
Save rjohnsondev/4605551 to your computer and use it in GitHub Desktop.
Add coloured labels to logging output :D
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
| 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