Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach an event/hook to all requests my Bottle app receives?

Tags:

python

bottle

I want to simply print a few line breaks (when my application is configured to be verbose) after every request so that I can easily separate them in my console. For example, my current verbose output looks something like this (There are way more debug print lines in the real application):

### 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

I want it to look like this:

### 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

Without manually adding the print in all my 100+ views.

Can this be done?

like image 305
Hubro Avatar asked Feb 19 '23 15:02

Hubro


1 Answers

For a quick hack, you could use the hooks plugin.

from bottle import hook

@hook('after_request')
def separator():
    print 3 * "\n",
like image 124
migonzalvar Avatar answered Apr 30 '23 05:04

migonzalvar