Recently at work, I've been working on writing a Python-based Subversion authorizer. Instead of using mod_authz_svn, which requires your groups and authorization rules be in a text file, we are using something that fits into our product's needs. Anyways, as I was doing this, I really needed a good way to log Subversion's request cycle. Your first thought would be to look at Apache access logs. Well, Subversion's mod_dav_svn uses Apache subrequests, when enabled of course, to communicate and since Apache doesn't log subrequests, I needed something custom.
In a previous blog entry, Using mod_python for Custom Apache/Subversion Authentication/Authorization, I discussed a few reasons why using mod_python was an excellent choice for writing Apache server extensions. That being said, I chose mod_python again for creating a way to log Apache's subrequests.
I will be using my need for logging the Subversion request cycle as the basis for the example code and Apache configuration you see. When using for your own benefit, you'll need to make changes to fit your environment. The good thing is that the example code is well documented so this should be easy. That being said, let's see how we can make mod_python get involved in our Apache communication:
... LoadModule python_module libexec/apache2/mod_python.so # Your path might be different ... <Location /repos> #Subversion configuration ... # mod_python configuration SetHandler mod_python # The interpreter to use PythonInterpreter main_interpreter # The mod_python handlers and their module/script that contains the mod_python handler functions PythonLogHandler svn_logger # Modify the sys.path to be able to locate your module/script PythonPath "['/opt/svn'] + sys.path" ... </Location> ...
The snippet above shows how I took an existing Subversion configuration and put the mod_python bits in there to help out with this venture. The Apache directives for mod_python are pretty straight forward but if you need more details, please refer to the mod_python documentation. Now on to the actual script used to log the Apache requests and subrequests for Subversion.
#!/usr/bin/env python # # -*- python -*- # # Simple script that uses mod_python to log the Subversion client requests, # including the subrequests made on the server side. import os, sys def loghandler(req): """ mod_python handler method taking an Apache request object. """ req_type = 'main' if req.main: req_type = 'subrequest' log = open('/tmp/svn_requests.log', 'a') log.write("[%s] %s->%s\n" % (req_type, req.method, req.uri)) log.close() return apache.OK # handler()
That is it. As you can see, the name of the function that you need to implement for mod_python to be happy is the name of the handler but all lower case and without the "Python" in front. (Note: mod_python has a few different ways to specify a handler, even allowing you to specify the method to be called eliminating the need to conform to standards if you want to be such a rebel.) Once we got mod_python's requirements out of the way, the code to log the requests was quite simple. In a production environment, where you don't want this to crash the application if there is a problem, you could easy use a try/except block to catch any errors while opening and writing to the file. Just make sure to return "apache.OK" to let Apache continue doing its thing.
I hope this simple example of using mod_python to log Apache subrequests was enlightening. I know it has helped me tremendously.



Comments
"In a previous blog entry,
"In a previous blog entry, Using mod_python for Custom Apache/Subversion Authentication/Authorization, I discussed a few reasons why using mod_python was an excellent choice for writing Apache server extensions. That being said, I chose mod_python again for creating a way to log Apache's subrequests."
Thanks for the information