如何将事件/挂钩附加到我的Bottle应用程序收到的所有请求中?

时间:2012-08-05 14:15:19

标签: python bottle

我想在每个请求之后简单地打印一些换行符(当我的应用程序配置为详细时),以便我可以在我的控制台中轻松地将它们分开。例如,我当前的详细输出看起来像这样(在实际应用程序中有更多的调试打印行):

### webserver->check_login: Checking session cookie for client at 127.0.0.1
### webserver->check_login: Credentials cookie was valid, client logged in
### webserver->render_secure_page: Rendering 'dashboard.html'
127.0.0.1 - - [05/Aug/2012 15:50:06] "GET /dashboard HTTP/1.1" 200 1728
### webserver->check_login: Checking session cookie for client at 127.0.0.1
### webserver->check_login: Credentials cookie was valid, client logged in
### webserver->render_secure_page: Rendering 'dashboard.html'
127.0.0.1 - - [05/Aug/2012 15:50:06] "GET /dashboard HTTP/1.1" 200 1728
### webserver->check_login: Checking session cookie for client at 127.0.0.1
### webserver->check_login: Credentials cookie was valid, client logged in
### webserver->render_secure_page: Rendering 'dashboard.html'
127.0.0.1 - - [05/Aug/2012 15:50:06] "GET /dashboard HTTP/1.1" 200 1728

我希望它看起来像这样:

### webserver->check_login: Checking session cookie for client at 127.0.0.1
### webserver->check_login: Credentials cookie was valid, client logged in
### webserver->render_secure_page: Rendering 'dashboard.html'
127.0.0.1 - - [05/Aug/2012 15:50:06] "GET /dashboard HTTP/1.1" 200 1728



### webserver->check_login: Checking session cookie for client at 127.0.0.1
### webserver->check_login: Credentials cookie was valid, client logged in
### webserver->render_secure_page: Rendering 'dashboard.html'
127.0.0.1 - - [05/Aug/2012 15:50:06] "GET /dashboard HTTP/1.1" 200 1728



### webserver->check_login: Checking session cookie for client at 127.0.0.1
### webserver->check_login: Credentials cookie was valid, client logged in
### webserver->render_secure_page: Rendering 'dashboard.html'
127.0.0.1 - - [05/Aug/2012 15:50:06] "GET /dashboard HTTP/1.1" 200 1728

无需在所有100多个视图中手动添加打印件。

可以这样做吗?

1 个答案:

答案 0 :(得分:8)

快速破解,您可以使用hooks plugin

from bottle import hook

@hook('after_request')
def separator():
    print 3 * "\n",