Save log record

Hello,
I want to know why it does not access the def login and I have seen that with import logging you can create a file and show the debugs that you have been putting in your code.
Something like that:

logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s',filename='errores.log',filemode='a')
logging.debug('message')

The issue is how do I know if it’s entering the def, and if it doesn’t (my case), what error is it leaving.
Locally it works for me but not on the hosting server, the fact is that I need to have records to know where it is failing.

Thank you.

Have a small main module that imports all the rest of the code and runs it.
Wrap all that code, including the imports, in a try except block that will log any exception that is raised using the exception method of the logging module.

Setup logging before the try block.

I understand the try except but the first thing you say about a main block I don’t know what you mean.

I not near my desktop to write this code example for you.

I mean code like this:

import logging
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s',filename='app.log',filemode='a')

try:
    logging.info('app starting')
    import app
    app.main()
    logging.info('app exiting')
except:
    logger.exception('App failed')

Which, becuase I have my module named app, creates this app.log:

2023-07-06 20:48:47,533 - INFO - app starting
2023-07-06 20:48:47,533 - ERROR - App failed
Traceback (most recent call last):
  File "/home/barry/tmpdir/a.py", line 6, in <module>
    import app
ModuleNotFoundError: No module named 'app'

I understand, but one thing, and I’m sorry I don’t understand much of this, does the part of the code that you show have to go inside my application startup file app.py?

try:
    logging.info('app starting')
    import app
    app.main()
    logging.info('app exiting')
except:
    logger.exception('App failed')

I have assumed a module named app.py but you can use any name for the module that python allows.

Hello, I mean how should I build the file.
For example: I have a file app.py where I have all the code and for example the def login() which is the initial one.

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method=='POST':
        logging.debug(request.form['username'])
        user = User(0,request.form['username'],request.form['password'])
        logging.debug(user)
        logged_user=ModelUser.login(db,user)
        if logged_user != None:
            if logged_user.password:
                login_user(logged_user)
                return redirect(url_for('home'))
            else:
                flash("invalid password")
                return render_template('auth/login.html')
        else:
            flash("User not found")
            return render_template('auth/login.html')
    else:
        return render_template('auth/login.html')

Where should I add the part of code you mention?

I cannot know as you are only showing us some of your application.
What is the full set of all files you currently have?
How do you run your application?

Note I used app as a place holder for what ever your application file name was.

My application only consists of the app.py file and its templates, you can find it at GitHub - arpanneupane19/Python-Flask-Authentication-Tutorial

Add a main.py with the code I provided then add this to your app.py

def main():
    app.run(debug=True)

Now run your application using:

python main.py

I’m going to put it to the test.

in local if I create the log file
I execute as indicated in python main.py but when I upload the new file app.py and the main.py to the hosting, it does not generate any log
In my hosting I also have an app.wsgi file with the code

import sys
sys.path.insert(0, '/var/www/html/flask_project')

from app import main as application

this code makes link with app.py

Should I modify that line to from app import main as application or something similar?

Sorry I do not know how that app.wsgi works.

You need someone who knows how that works to advise.

My guess is that you should setup logging in app.wsgi.
But I’m not sure of the best way to catch exception.

I understand.
It is a very curious thing that happens to me with two similar applications and in two different hostings, the server has something in part of the code that it does not like, although locally it is correct and functional.

Do you have access to the server logs?
I would hope that any errors from running your code are logged somewhere.

it is a VPS server
ubuntu 22.04

You need to research where the VPS server writes its logs then.