I'm creating a Webservice using BaseHTTPServer.HTTPServer
I would like to log the following to be logged to a file rather than to the console. But I have not managed to find a way to do so yet.
10.23.23.19 - - [29/Nov/2013 08:39:06] "GET / HTTP/1.1" 200 -
10.23.23.19 - - [29/Nov/2013 08:39:06] "POST / HTTP/1.1" 200 -
10.24.20.14 - - [29/Nov/2013 08:39:27] "POST / HTTP/1.1" 200 -
10.24.20.14 - - [29/Nov/2013 08:39:31] "POST / HTTP/1.1" 200 -
My Code looks like this:
from BaseHTTPServer import HTTPServer
from pysimplesoap.server import SoapDispatcher, SOAPHandler
.
# The rest of the code
.
.
httpd = HTTPServer(("", 8059),SOAPHandler)
httpd.dispatcher = dispatcher
httpd.serve_forever()
I'm using Python 2.6
If you read the doc or source code for BaseHTTPRequestHandler you'll find that all logging goes thru BaseHTTPRequestHandler.log_message()
, which docstring explicitely specifies:
Log an arbitrary message.
This is used by all other logging functions. Override it if you have specific logging wishes.
So the solution is obviously to leave .send_response()
alone (obviously you want your response to be sent), and replace .log_message()
with either a call to a proper logger (cf http://docs.python.org/2/library/logging.html) which is the clean and flexible way to to handle logging in Python or if you just want an quick hack a write to a filesystem file.