Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 468573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:41:43+00:00 2026-05-12T23:41:43+00:00

In web2py, is there a way to have a piece of common code be

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:41:43+00:00Added an answer on May 12, 2026 at 11:41 pm

    You could simply put your piece of logging code in the model definition file, models/db.py, or in your controller controllers/default.py like this:

    with open("mylog.log", "at") as f:
        f.write(repr(request))
    
    def index():
        # index controller definition
    
    # ... rest of the code
    

    or, if you need functions or classes to be defined:

    # --------------------------
    # Log part:
    # --------------------------
    
    def my_log(request):
        with open("mylog.log", "at") as f:
            f.write(repr(request))
    
    my_log(request)
    
    # --------------------------
    # Controllers part:
    # --------------------------
    
    def index():
        # index controller definition
    
    # ... rest of the code
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a web2py way of displaying images from a database table? Example: The
I am building an app using Web2py framework... I don't want to have to
I am studying web2py. I read example open source code. In one application (storpy),
As a way of evaluating Web2Py I tried to reimplement a web page in
I have dozens of tables in an existing MSSQL database all with autonumber ID
What is the recommended way to log application-specific messages in web2py? I found the
In a web2py view, how do I comment out server-side code? In ASP.NET, I
Web2py docs have two methods for inserting into a database db.tbl[0] = newRow and
Is using blueprint with web2py common or recommended? Does blueprint conflict with the default
I want to make a simple FORM (not SQLFORM) in web2py with two fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.