Niklas D.
Niklas D.

Reputation: 11

How can I log errors from uswgi + flask app?

I'm new to wsgi and flask and want to check the logs. However I cannot print the logs to a file. I used this tutorial to set up my project. I would like to log things like print statements in python and errors that occue.

this is my config for usgwi server.ini:

[uwsgi]
module = wsgi:app

master = false
processes = 1

socket = myproject.sock
socket = stats.sock
chmod-socket = 660
vacuum = true

die-on-term = true

plugins-dir = /usr/lib/uwsgi/plugins
plugins = logfile


logger = file:logs.log
logger-req = file:requests.log
stats = myproject.sock

logs.log and requests.log never get filled (master=false and process=1 because i am using tensorflow in the app)

then i have the wsgi.py

from main import app

if __name__ == "__main__":
    app.run()

then the flask app main.py

app = Flask(__name__, static_url_path='/www')

@app.route('/')
def send_index():
    print("I want to print this to logs")
    x = 1 / 0 # I would like this to throw a ZeroDivisionError and send it to logs
    return send_from_directory('www', 'index.html')

if __name__ == '__main__':
     app.run(host='0.0.0.0')

Upvotes: 1

Views: 961

Answers (1)

rajat yadav
rajat yadav

Reputation: 383

One way is to use logging within the app using the 'logging' package. A few quick hints:

logging.basicConfig(filename='record.log', level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')

Should be called in main.py after your app is declared. You may also use:

current_app.logger.info("Your message here")

to log specific events.

Upvotes: 1

Related Questions