-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/ghi-#3-logger' into develop
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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" |
This file contains 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
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 |
This file contains 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
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 |