[Twisted-Python] Log output formatting (was Re: logging question)

Tim Allen screwtape at froup.com
Tue Nov 29 23:07:22 EST 2011


On Tue, Nov 29, 2011 at 10:44:21AM -0800, Don Dwiggins wrote:
> Looking at the source of log.py, I'm at a bit of a loss to reconcile 
> these two forces.  Certainly, the two log observers implemented there 
> use textFromEventDict, but one could create a different observer that 
> does things entirely differently.
> 
> The best idea I can come up with is, in the documentation for msg, refer 
> to the documentation for the chosen log observer; then, in each log 
> observer's documentation, describe how the formatting is done, either 
> explicitly, or by reference to textFromEventDict.  (And maybe in the 
> documentation for ILogObserver, recommend strongly that each 
> implementation be explicit about message formatting.)  There should 
> probably also be something in the logging howto.
> 
> Any better suggestions?

While not every log observer needs to flatten a log event dict into
a string, that particular approach is probably common enough that it
deserves a simple solution. I think, in my ideal world, there would be
a LogMessage class that inherits from dict, with a __str__ method that
looks something like:

    def __str__(self):
	if "msg" in self:
	    return self["msg"]
	elif "format" in self:
	    return self["format"] % self
	else return dict.__str__(self)

If the standard Twisted logging functions automatically constructed
LogMessage instances from dict instances, it should be easy enough for
future ILogObserver implementations to do the right thing (by just
calling str(msg)). Of course, they could also do more sophisticated
things like pulling particular keys out of the message to set
observer-specific message properties (like syslog channel and severity,
etc.)

The documentation for the standard Twisted logging functions could then
just point to the LogMessage documentation to describe what goes into
a log event and how it gets interpreted, with a footnote to the effect
that different log observers can handle the same message in different
ways.



More information about the Twisted-Python mailing list