2012-07-28 73 views
3

有没有任何选项瓶侧,可以保持服务器像WSGIRef粘贴输出一个线路收到每个请求?有没有办法让Bottle服务器不那么冗长?

注:我知道有一个安静的选择,但我不希望整个器件的应用保持沉默,只是请求日志。

它非常快速地变得非常混乱,尤其是考虑到我想打印调试信息时不时地混乱。下面是一个单一的页面负载的输出,它可能会得到很多大时,我的项目增长了一点:

 
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 
+0

。 – jordanm 2012-07-28 02:13:58

+0

虽然这是为了发展 - Apache有点硬核。此外,Bottle的重新启动程序将不起作用 – Hubro 2012-07-28 02:18:10

+3

如果没有人有真正的解决方案,您可以将输出传递给'grep -Ev'(GET | POST)' – jordanm 2012-07-28 02:20:22

回答

1

我在过去做了这样的事情。对于不同类型的服务器,您可以覆盖日志处理程序以过滤掉您不需要的日志。我复制了Bottle中的代码并创建了自己的ServerAdapter,下面是WSGI服务器的代码。熟悉安静的功能,覆盖log_request函数我自己的处理程序类并覆盖原始的log_request,然后根据传递给函数的响应代码过滤出消息。

我从内置的BaseHTTPServer模块中复制了log_request函数并添加了if语句。

那么当你开始瓶,如果你使用的mod_wsgi相反,阿帕奇将拆分访问和错误日​​志传递客户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) 
相关问题