[Twisted-Python] Twistd logging

Lucas Taylor ltaylor.volks at gmail.com
Wed Dec 9 15:34:24 MST 2009


On 12/9/09 11:14 AM, Landreville wrote:
> 
> 
> On Wed, Dec 9, 2009 at 12:26 PM, <exarkun at twistedmatrix.com
> <mailto:exarkun at twistedmatrix.com>> wrote:
> 
>     On 8 Dec, 09:04 pm, landreville at deadtreepages.com
>     <mailto:landreville at deadtreepages.com> wrote:
>     >Is there a way I can customize the format of log file?
>     >I want to add more information to each line (about the xmlrpc method
>     >being
>     >called), but I can't find where these lines get written out. I know I
>     >can
>     >print a message to the log in my _getFunction, but I would prefer to
>     >have
>     >one line per request.
> 
>     Are you talking about the CLF request log or the main log file (normally
>     called "twistd.log")?
> 
>     The former can be controlled by overriding the "log" method of Site.
>     The latter is controlled by the general log observer(s) which is
>     installed.  You can read about customizing the log observer using .tac
>     files in the online documentation:
> 
>      http://twistedmatrix.com/documents/current/core/howto/application.html
> 
> 
> I'm talking about the main log file (twistd.log) that is created. I want
> to customize the lines that are written to it, the document only tells
> me how to use a different observer, not how to format the lines that are
> being output by twistd.
> 
> I would like to change the output from:
> 2009-12-09 12:59:01-0500 [-] 127.0.0.1 - - [09/Dec/2009:17:59:01 +0000]
> "POST /XMLRPC HTTP/1.0" 200 647 "-" "xmlrpclib.py/1.0.1
> <http://xmlrpclib.py/1.0.1> (by www.pythonware.com
> <http://www.pythonware.com>)
> to include "xmlrpc method: system.listMethods" at the end (or something
> similar to include the methd name".
> 
> I see in the observers an eventDict is passed in to be output in the
> log, but where does that eventDict get created/passed in?
> 

The output you describe is the CLF request log generated by the Site
instance (via twisted.web.http.HTTPFactory.log). You can do as suggested
and override the "log" method on your Site instance. This will give you
access to the request and allow you to extract the xmlrpc method.

Something like...

class Site(server.Site):
    def log(self, request):
        request.content.seek(0,0)
        args, functionPath = xmlrpclib.loads(request.content.read())

	# Keep the CLF line
	server.Site.log(self, request)

	# Add your own info on another line
        if hasattr(self, "logFile"):
            self.logFile.write('xmlrpc method: %s\n' % (functionPath,))

You did say you would prefer to have one line per request, so you might
want to just recreate the log method from HTTPFactory here and make
appropriate modifications (calculating request date/time and adding your
xmlrpc info).







More information about the Twisted-Python mailing list