Last active
March 27, 2016 16:14
-
-
Save yoavst/fa441a70bbbe65662437 to your computer and use it in GitHub Desktop.
Skillz Logging "Framework"
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
LEVEL_VERBOSE = 1 | |
LEVEL_INFO = 2 | |
LEVEL_DEBUG = 3 | |
LEVEL_ERROR = 4 | |
TITLES = {LEVEL_VERBOSE: "V", LEVEL_INFO: "I", LEVEL_DEBUG: "D", LEVEL_ERROR: "E"} | |
DEFAULT_TAGS = ["Manager", "gameinfo"] | |
class Logger(object): | |
separator = ' ' | |
def __init__(self, logging, level=LEVEL_VERBOSE, enabled_tags=None): | |
self._log_level = level | |
self._logging = logging | |
self._enabled_tags = enabled_tags | |
def v(self, tag, *args): | |
if args and self.assure_tags(tag): | |
self._level_log(LEVEL_VERBOSE, Logger._to_str(args)) | |
def i(self, tag, *args): | |
if args and self.assure_tags(tag): | |
self._level_log(LEVEL_INFO, Logger._to_str(args)) | |
def d(self, tag, *args): | |
if args and self.assure_tags(tag): | |
self._level_log(LEVEL_DEBUG, Logger._to_str(args)) | |
def e(self, tag, *args): | |
if args and self.assure_tags(tag): | |
self._level_log(LEVEL_ERROR, Logger._to_str(args)) | |
def _level_log(self, level, data): | |
if self._log_level <= level: | |
self._log("[" + TITLES[level] + "] " + data) | |
def _log(self, data): | |
self._logging(data) | |
def assure_tags(self, tag): | |
if self._enabled_tags: | |
return tag in DEFAULT_TAGS or tag in self._enabled_tags | |
return True | |
@staticmethod | |
def _to_str(args): | |
return Logger.separator.join(map(lambda it: str(it), args)) | |
class LoggingObject(object): | |
@property | |
def logger(self): | |
return logger() | |
@property | |
def logging_tag(self): | |
""" | |
:rtype: string | |
:return: the tag for logging | |
""" | |
return self.__class__.__name__ | |
def v(self, *args): | |
self.logger.v(self.logging_tag, *args) | |
def i(self, *args): | |
self.logger.v(self.logging_tag, *args) | |
def d(self, *args): | |
self.logger.v(self.logging_tag, *args) | |
def e(self, *args): | |
self.logger.v(self.logging_tag, *args) | |
logger_obj = None # type: Logger | |
def set_logger(new_logger): | |
global logger_obj | |
logger_obj = new_logger | |
return logger() | |
def logger(): | |
return logger_obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment