Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Created September 28, 2016 15:05
Show Gist options
  • Select an option

  • Save vyach-vasiliev/e04f927316fdf7b1f2a5cd11fc2e1d66 to your computer and use it in GitHub Desktop.

Select an option

Save vyach-vasiliev/e04f927316fdf7b1f2a5cd11fc2e1d66 to your computer and use it in GitHub Desktop.
Small Color Logger for Python
# -*- coding: utf-8 -*-
from __future__ import print_function
'''
------------------------------------------------------------------------------
Small Color Logger for Python.
For test run it in consol >> python sc_logger.py
------------------------------------------------------------------------------
How use:
from sc_logger import Logger
log = Logger() # initialize logger
log.DEBUG = True # On mode logger (default)
log("Text")
log.info("Text")
------------------------------------------------------------------------------
Here used the part code of these authors:
@joeld (stackoverflow.com/users/19104/joeld)
@abimelex (stackoverflow.com/users/1059828/abimelex)
@vyach.vasiliev (vyach.vasiliev <at> google <dot> com)
------------------------------------------------------------------------------
The MIT License
Copyright (c) 2016 Vyacheslav Vasiliev (vyach.vasiliev\аt\gmail\dоt\com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
------------------------------------------------------------------------------
'''
class BCOLORS:
def __init__(self): pass
HEADER = '\033[95m'
INFO = '\033[94m'
SUCCESS = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
NONE = None
class Logger:
def __init__(self):
self.DEBUG = True
def __call__(self, *args, **kwargs):
self._print(BCOLORS.NONE, *args, **kwargs)
def head(self, *args, **kwargs):
self._print(BCOLORS.HEADER, *args, **kwargs)
def info(self, *args, **kwargs):
self._print(BCOLORS.INFO, *args, **kwargs)
def success(self, *args, **kwargs):
self._print(BCOLORS.SUCCESS, *args, **kwargs)
def warn(self, *args, **kwargs):
self._print(BCOLORS.WARNING, *args, **kwargs)
def bold(self, *args, **kwargs):
self._print(BCOLORS.BOLD, *args, **kwargs)
def under(self, *args, **kwargs):
self._print(BCOLORS.UNDERLINE, *args, **kwargs)
def _print(self, color, *args, **kwargs):
if self.DEBUG:
if not color: color = ''
print(color, end="")
print(*args, **kwargs)
print(BCOLORS.ENDC, end="")
if __name__ == '__main__':
log = Logger()
log.head('Hi, I am Logger! Nice to meet you!')
log.head('The following methods are available:')
print()
log('log("Text")')
log.bold('log.bold("Text")')
log.head('log.head("Text")')
log.info('log.info("Text")')
log.success('log.success("Text")')
log.warn('log.warn("Text")')
log.under('log.under("Text")')
print()
log.head('Enjoy! ;-)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment