Skip to content

Instantly share code, notes, and snippets.

@anti1869
Created January 4, 2016 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anti1869/d6c14df99f14dd5742cc to your computer and use it in GitHub Desktop.
Save anti1869/d6c14df99f14dd5742cc to your computer and use it in GitHub Desktop.
How To Add Colour To Logs And Terminal
#!/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