In web2py, is there a way to have a piece of common code be executed before all controllers are called?
For example, I want to add some code that will log client IPs to a log of requests to enable analysis. I could simply make the first line of all my controllers be something like response = RequestBase(request) but I’m curious to know if this is a problem that’s already been solved through some other mechanisms.
You could simply put your piece of logging code in the model definition file,
models/db.py, or in your controllercontrollers/default.pylike this:or, if you need functions or classes to be defined:
Of course,
repr(request)is not how you want it, but you get the idea: from there you can log any information you like before the controllers are called (they are just defined at this stage).The server already maintains a log in the root directory, in
httpserver.log.