Question

Is there any option on the Bottle side that can keep servers like WSGIRef and Paste from outputting a line for every request received?

NB: I know there's a quiet option, but I don't want the entire appication to be silent, just the request log.

It gets very messy very quickly, especially considering that I'd like to print debug information now and then and it just gets lost in the chaos. Here's the output for a single page-load, and it'll probably get a lot bigger when my project grows a little:

Bottle server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8080/
Hit Ctrl-C to quit.

localhost - - [28/Jul/2012 04:05:59] "GET /clients HTTP/1.1" 200 3129
localhost - - [28/Jul/2012 04:05:59] "GET /static/css/main.css HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:05:59] "GET /static/js/jquery-1.7.2.js HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:05:59] "GET /static/js/jquery.cookie.js HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:05:59] "GET /static/js/jquery.qtip.min.js HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:05:59] "GET /static/js/showdown.js HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:05:59] "GET /static/js/proj.js HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:05:59] "GET /static/css/reset.css HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:06:00] "GET /static/images/flag_gb.png HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:06:00] "GET /static/images/flag_no.png HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:06:00] "GET /static/images/icon_add.png HTTP/1.1" 304 0
localhost - - [28/Jul/2012 04:06:00] "GET /favicon.ico HTTP/1.1" 404 742
Was it helpful?

Solution

I have done something like this in the past. For the different kind of servers you can overwrite the log handler to filter out logs you dont want. I copied the code from Bottle and make my own ServerAdapter, Below is the code for a WSGI server. Similiar to the quiet feature overriding the log_request function my own handler class and overriding the original log_request then filtering out the message based on the response code passed to the function.

i copied the log_request function from the built in BaseHTTPServer module and added the if statement.

then when you start bottle, pass it your customer serverAdapter

from bottle import route, run, template
import bottle

@route('/hello/:name')
def index(name='World'):
    print "Debug Print Statement"
    return template('<b>Hello {{name}}</b>!', name=name)

class WSGIRefServer(bottle.ServerAdapter):
    def run(self, handler): # pragma: no cover
        from wsgiref.simple_server import make_server, WSGIRequestHandler

        class LogHandler(WSGIRequestHandler):
            def log_request(self, code='-', size='-'):
                """Log an accepted request.

                This is called by send_response().

                """
                if code not in  ["200", "304"]:
                    self.log_message('"%s" %s %s',
                                     self.requestline, str(code), str(size))

        self.options['handler_class'] = LogHandler
        srv = make_server(self.host, self.port, handler, **self.options)
        srv.serve_forever()


run(host='localhost', server=WSGIRefServer, port=8080)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top