Skip to content

Commit

Permalink
Different approach -- two loggers
Browse files Browse the repository at this point in the history
The logger that the 'debug' module logs on is now vistrails.logger. It
is internal and API users should NOT configure it (change level or
attach filters/handlers to it). It is controlled by the 'debug' module.

This internal logger re-logs to a 'vistrails' logger, which is meant to
be configured by API users if needed -- changing the level of this one
won't break anything.
  • Loading branch information
remram44 committed Aug 27, 2014
1 parent 54e770a commit 0d6e563
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions vistrails/core/debug.py
Expand Up @@ -133,24 +133,16 @@ def emit(self, record):

################################################################################

class LevelCheckerLogger(logging.Logger):
def callHandlers(self, record):
"""Variant that checks the parents' levels when propagating.
"""
for hdlr in self.handlers:
if record.levelno >= hdlr.level:
hdlr.handle(record)
if (self.propagate and self.parent and
self.parent.isEnabledFor(record.levelno)):
c = self.parent
while c:
for hdlr in c.handlers:
if record.levelno >= hdlr.level:
hdlr.handle(record)
if not c.propagate:
c = None #break out
else:
c = c.parent
class LoggerHandler(logging.Logger):
"""A logging Handler Handler re-logs on a specified Logger.
"""
def __init__(self, logger):
logging.Handler.__init__(self)
self.logger = logger

def emit(self, record):
if self.logger.isEnabledFor(record.levelno):
self.logger.handle(record)

################################################################################

Expand Down Expand Up @@ -203,16 +195,8 @@ def make_logger(self, f=None):
We will configure log so it outputs to both stderr and a file.
"""
# Setup root logger
logging._acquireLock()
try:
oldLoggerClass = logging.getLoggerClass()
logging.setLoggerClass(LevelCheckerLogger)
self.logger = logging.getLogger('vistrails')
logging.setLoggerClass(oldLoggerClass)
finally:
logging._releaseLock()
assert isinstance(self.logger, LevelCheckerLogger)
# Internal logger, the one we log on
self.logger = logging.getLogger('vistrails.logger')

self.logger.setLevel(logging.DEBUG)
self.format = logging.Formatter("%(asctime)s %(levelname)s:\n%(message)s")
Expand All @@ -234,6 +218,12 @@ def make_logger(self, f=None):
self.console.setLevel(logging.WARNING)
self.logger.addHandler(self.console)

# We also propagate to a second logger, that API users might want to
# configure
self.visible_logger = logging.getLogger('vistrails')
self.logger.propagate = False
self.logger.addHandler(LoggerHandler(self.visible_logger))

# if system.python_version() <= (2,4,0,'',0):
# raise VersionTooLow('Python', '2.4.0')

Expand Down

0 comments on commit 0d6e563

Please sign in to comment.