Created
January 4, 2016 10:41
-
-
Save anti1869/d6c14df99f14dd5742cc to your computer and use it in GitHub Desktop.
How To Add Colour To Logs And Terminal
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
#!/usr/bin/env python | |
""" | |
Colour logs, printing colour escape codes and some ASCII. | |
You need to ``pip install colorlog`` to run this. | |
""" | |
import logging | |
from logging.config import dictConfig | |
logger = logging.getLogger(__name__) | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'colored': { | |
'()': 'colorlog.ColoredFormatter', | |
'format': "%(log_color)s%(levelname)-8s%(reset)s %(blue)s[%(name)s:%(lineno)s] %(white)s%(message)s" | |
}, | |
}, | |
'handlers': { | |
'console': { | |
'level': 'DEBUG', | |
'class': 'logging.StreamHandler', | |
'formatter': 'colored', | |
}, | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['console'], | |
'level': 'DEBUG', | |
}, | |
} | |
} | |
dictConfig(LOGGING) | |
PIC = r""" | |
.- <O> -. .-====-. ,-------. .-=<>=-. | |
/_-\'''/-_\ / / '' \ \ |,-----.| /__----__\ | |
|/ o) (o \| | | ')(' | | /,'-----'.\ |/ (')(') \| | |
\ ._. / \ \ / / {_/(') (')\_} \ __ / | |
,>-_,,,_-<. >'=jf='< `. _ .' ,'--__--'. | |
/ . \ / \ /'-___-'\ / :| \ | |
(_) . (_) / \ / \ (_) :| (_) | |
\_-----'____--/ (_) (_) (_)_______(_) |___:|____| | |
\___________/ |________| \_______/ |_________| | |
""" # noqa | |
def esc(*x): return '\033[' + ';'.join(x) + 'm' | |
red = esc("31") | |
green = esc("32") | |
cyan = esc("36") | |
reset = esc("0") | |
def main(): | |
logger.info("Starting app...") | |
logger.warning("No loitering here!") | |
# You also could print with escape codes to change output codes | |
print(cyan) | |
print(PIC) | |
print("%sPress Ctrl+C to exit" % green) | |
print(reset) | |
try: | |
while True: | |
pass | |
except KeyboardInterrupt: | |
logger.info("Normal exit by Ctrl+C") | |
print(red + "Bye") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment