Skip to content

Commit

Permalink
Merge branch 'feature/ghi-#3-logger' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
arcticicestudio committed Jan 7, 2017
2 parents 528d171 + 5a0d6bd commit c56a719
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions snowsaw/logging/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Provides a custom logging class with level color support.
"""
from .logger import Logger
from .color import Color
from .level import Level
11 changes: 11 additions & 0 deletions snowsaw/logging/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Color(object):
"""
Terminal escape sequences for colored logging.
"""
NONE = ""
RESET = "\033[0m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
10 changes: 10 additions & 0 deletions snowsaw/logging/level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Level(object):
"""
Logging level names as numeric values.
"""
NOTSET = 0
DEBUG = 10
LOWINFO = 15
INFO = 20
WARNING = 30
ERROR = 40
70 changes: 70 additions & 0 deletions snowsaw/logging/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sys

from ..util.singleton import Singleton
from ..util.compat import with_metaclass

from .color import Color
from .level import Level


class Logger(with_metaclass(Singleton, object)):
"""
A custom logging class with level color support.
"""
def __init__(self, level=Level.LOWINFO):
self.set_level(level)

def set_level(self, level):
self._level = level

def log(self, level, message):
if level >= self._level:
print("{}{}{}".format(self._color(level), message, self._reset()))

def debug(self, message):
self.log(Level.DEBUG, message)

def lowinfo(self, message):
self.log(Level.LOWINFO, message)

def info(self, message):
self.log(Level.INFO, message)

def warning(self, message):
self.log(Level.WARNING, message)

def error(self, message):
self.log(Level.ERROR, message)

def _color(self, level):
"""
Gets a color according to a level.
:param level: The level to get the color of
:return: The color of the specified level
"""
if not sys.stdout.isatty():
return ""
elif level < Level.DEBUG:
return ""
elif Level.DEBUG <= level < Level.LOWINFO:
return Color.MAGENTA
elif Level.LOWINFO <= level < Level.INFO:
return Color.BLUE
elif Level.INFO <= level < Level.WARNING:
return Color.GREEN
elif Level.WARNING <= level < Level.ERROR:
return Color.YELLOW
elif Level.ERROR <= level:
return Color.RED

def _reset(self):
"""
Gets a reset color.
:return: The reset color
"""
if not sys.stdout.isatty():
return ""
else:
return Color.RESET

0 comments on commit c56a719

Please sign in to comment.