Skip to content
Permalink
Browse files

No more filtering in Logger.emit(); add a new LogLevelFilteringLogObs…

…erverWrapper which handles that.

Get rid of "_enabled" truthy thing for testing whether a logger will log; it's totally unused.  (eg. log.error_enabled)

git-svn-id: https://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk@11339 e27351fd-9f3e-4f54-a53b-843176b1656c
  • Loading branch information...
wsanchez committed Jun 13, 2013
1 parent 527c19d commit f332529dae87a5d03075992fe0a717fc9e8c6a32
Showing with 40 additions and 28 deletions.
  1. +40 −13 twext/python/log.py
  2. +0 −15 twext/python/test/test_log.py
@@ -50,12 +50,12 @@ def oops(self, data):
#
# TODO List:
#
# * Filter in observers, not emit() - use a wrapper observer
# * Expose the default log observer (TheLogPublisher)
# * Specifically, expose addObserver and removeObserver so one can register other observers
# * Check the unicode situation for sanity
# * Change the default log observer to something non-legacy
# * Register a legacy observer with Twisted's logging that forwards to this module
# * Monkey patch logging in Twisted to use our LegacyLogger to sprinkle betterness everywhere
#

__all__ = [
@@ -69,6 +69,7 @@ def oops(self, data):
"ILogObserver",
"ILegacyLogObserver",
"LogPublisher",
"LogLevelFilteringLogObserverWrapper",
"LegacyLogObserverWrapper",
#"StandardIOObserver",
]
@@ -329,10 +330,7 @@ def emit(self, level, format=None, **kwargs):
logger = self,
)
#level = LogLevel.error
return

# FIXME: Filtering should be done by the log observer(s)
if not self.willLogAtLevel(level):
# FIXME: continue to emit?
return

event = kwargs
@@ -396,7 +394,6 @@ def setLevel(self, level):
setLogLevelForNamespace(self.namespace, level)


# FIXME: get rid of this
def willLogAtLevel(self, level):
"""
@param level: a L{LogLevel}
@@ -468,7 +465,6 @@ def will_emit(self):
log_emit.__doc__ = doc

setattr(Logger, level.name, log_emit)
setattr(Logger, level.name + "_enabled", property(will_emit))

for level in LogLevel.iterconstants():
bindEmit(level)
@@ -502,15 +498,15 @@ def __call__(event):
@implementer(ILogObserver)
class LogPublisher(object):
"""
Log publisher that fans out events to other observers.
I{ILogObserver} that fans out events to other observers.
Keeps track of a set of L{ILogObserver} objects and forwards
events to each.
"""
log = Logger()

def __init__(self):
self._observers = set()
def __init__(self, *observers):
self._observers = set(observers)


@property
@@ -560,11 +556,41 @@ def __call__(self, event):



@implementer(ILogObserver)
class LogLevelFilteringLogObserverWrapper(object):
"""
L{ILogObserver} that wraps another L{ILogObserver}, but does not
forward events which have a L{LogLevel} lower than is configured
for the event's namespace.
"""

def __init__(self, observer):
"""
@param observer: an L{ILogObserver} to which this observer
will forward events.
"""
self.observer = observer


def eventShouldForward(self, event):
if event["log_level"] >= logLevelForNamespace(event["log_namespace"]):
return True
else:
return False


def __call__(self, event):
if self.eventShouldForward(event):
self.observer(event)



@implementer(ILogObserver)
class LegacyLogObserverWrapper(object):
"""
L{ILogObserver} that wraps an L{ILegacyLogObserver}.
"""

def __init__(self, legacyObserver):
"""
@param legacyObserver: an L{ILegacyLogObserver} to which this
@@ -612,9 +638,10 @@ def __str__(self):



TheLogPublisher = LogPublisher()
TheLogPublisher.addObserver(LegacyLogObserverWrapper(twistedLogMessage))

TheLegacyLogObserver = LegacyLogObserverWrapper(twistedLogMessage)
TheFilteredLogPublisher = LogPublisher(TheLegacyLogObserver) # Add post-filtering observers here
TheFilteringLogObserver = LogLevelFilteringLogObserverWrapper(TheFilteredLogPublisher)
TheLogPublisher = LogPublisher(TheFilteringLogObserver) # Add pre-filtering observers here


######################################################################
@@ -323,21 +323,6 @@ def test_willLogAtLevel(self):
self.assertTrue(log.willLogAtLevel(level), (level, log.level()))


def test_logMethodTruthiness_Logger(self):
"""
Logger's log level functions/methods have true/false
value based on whether they will log.
"""
log = Logger()

for level in LogLevel.iterconstants():
enabled = getattr(log, level.name + "_enabled")
if enabled:
self.assertTrue(log.willLogAtLevel(level))
else:
self.assertFalse(log.willLogAtLevel(level))


def test_logInvalidLogLevel(self):
"""
Test passing in a bogus log level to C{emit()}.

0 comments on commit f332529

Please sign in to comment.
You can’t perform that action at this time.