Skip to content
Permalink
Browse files

log.emit() now takes a format, instead of a message.

  • Loading branch information...
wsanchez committed Jun 10, 2013
1 parent 780d386 commit bedb21e1bf0d2ec7394a51988c1baead5b022647
Showing with 37 additions and 14 deletions.
  1. +22 −11 twext/python/log.py
  2. +15 −3 twext/python/test/test_log.py
@@ -50,6 +50,7 @@ def oops(self):
# TODO List:
#
# * Replace message argument with format argument
# * Filter in observers, not emit()
#

__all__ = [
@@ -244,19 +245,21 @@ def __repr__(self):
return "<%s %r>" % (self.__class__.__name__, self.namespace)


def emit(self, level, message=None, **kwargs):
def emit(self, level, format=None, **kwargs):
"""
Emit a log message to all log observers at the given level.
@param level: a L{LogLevel}
@param message: a message
@param format: a message format using PEP3101 formatting. All
variables in C{kwargs} are available.
@param kwargs: additional keyword parameters to include with the
message.
"""
if level not in LogLevel.iterconstants():
raise InvalidLogLevelError(level)
self.failure(Failure(InvalidLogLevelError(level)))
level = LogLevel.error

# FIXME: Filtering should be done by the log observer(s)
if not self.willLogAtLevel(level):
@@ -274,10 +277,6 @@ def emit(self, level, message=None, **kwargs):
if level in pythonLogLevelMapping:
kwargs["logLevel"] = pythonLogLevelMapping[level]

if message:
kwargs["legacyMessage"] = message
kwargs["format"] = "%(legacyMessage)s"

prefix = "[%(namespace)s#%(levelName)s] "

if "failure" in kwargs:
@@ -288,8 +287,20 @@ def emit(self, level, message=None, **kwargs):
why = "Unhandled Error"
kwargs["why"] = "%s%s" % (prefix % kwargs, why)

if "format" in kwargs:
kwargs["format"] = "%s%s" % (prefix, kwargs["format"])
if format:
kwargs["log_format"] = format

#
# Create an object that implements __str__() in order to
# defer the work of formatting until it's needed by a
# legacy log observer.
#
class LegacyFormatStub(object):
def __str__(self):
return format.format(**kwargs)

kwargs["format"] = prefix + "%(log_legacy)s"
kwargs["log_legacy"] = LegacyFormatStub()

twistedLogMessage(**kwargs)

@@ -390,8 +401,8 @@ def bindEmit(level):
#
# Attach methods to Logger
#
def log_emit(self, message=None, **kwargs):
self.emit(level, message, **kwargs)
def log_emit(self, format=None, **kwargs):
self.emit(level, format, **kwargs)

def will_emit(self):
return self.willLogAtLevel(level)
@@ -72,7 +72,7 @@ def __init__(self, state=None):


def __str__(self):
return "<LogComposedObject %s>" % (self.state,)
return "<LogComposedObject {state}>".format(state=self.state)



@@ -126,7 +126,7 @@ def test_sourceAvailableForFormatting(self):
"""
obj = LogComposedObject("hello")
log = obj.log
log.error(format="Hello. %(source)s")
log.error("Hello. {source}")
stuff = twistedLogging.textFromEventDict(log.eventDict)
self.assertIn("Hello. <LogComposedObject hello>", stuff)

@@ -137,7 +137,7 @@ def test_basic_Logger(self):
Logger.
"""
for level in LogLevel.iterconstants():
message = "This is a %s message" % (level.name,)
message = "This is a {level} message".format(level=level.name)

log = TestLogger()
method = getattr(log, level.name)
@@ -306,6 +306,18 @@ def test_logMethodTruthiness_Logger(self):
self.assertFalse(log.willLogAtLevel(level))


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

log.emit("*bogus*")

errors = self.flushLoggedErrors(InvalidLogLevelError)
self.assertEquals(len(errors), 1)


def test_legacy_msg(self):
"""
Test LegacyLogger's log.msg()

0 comments on commit bedb21e

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