Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask logging with Foreman

I'm trying to set up a Heroku-ready Flask app, but I can't figure out how to turn on logging.

Without Foreman, I could create a helloworld app as described in the Flask tutorial:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    app.logger.debug('A value for debugging')
    app.logger.warning('A value for warning')
    return "Hello World!"

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

start it like so:

python hello.py

and have logging in stdout.

When I follow the Heroku tutorial, however, there's no app.run line:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    app.logger.debug('A value for debugging')
    app.logger.warning('A value for warning')
    return 'Hello World!'

And so I can't figure out how to run in debug mode and/or get logging output:

foreman start -p 5000

Procfile:

web: gunicorn hello:app
like image 764
Yarin Avatar asked Dec 08 '13 18:12

Yarin


People also ask

How do you log data on a Flask?

To start with logging in Flask, first import the logging module from Python. This logger module comes out of the box from the Python installation and does not need configuration. The Python logging module logs events based on pre-defined levels. The recorded log events are known as log records.

How do I add screen logging to my Flask application?

We first initialize a Flask application class and define the static and template folders. Then we define a route ('/') and tell the application that it should render index. html. The last line tells the application to expose itself on port 5000.

What is Flask_env?

The environment is used to indicate to Flask, extensions, and other programs, like Sentry, what context Flask is running in. It is controlled with the FLASK_ENV environment variable and defaults to production . Setting FLASK_ENV to development will enable debug mode.


1 Answers

The default logging configuration for Flask apps is different in debug vs. production mode.

In your first example you are in debug mode. In this case Flask defines a logging handlers that logs all messages with level logging.DEBUG or higher to stderr.

The second example is not in debug mode. When debug mode is not enabled Flask creates a logger object but does not add any handlers to it, so nothing is printed.

For Foreman and Heroku you need logs to be sent to stdout or stderr, so all you need to do is add a StreamHandler with the logging level of your choice:

import os
from flask import Flask

app = Flask(__name__)

# log to stderr
import logging
from logging import StreamHandler
file_handler = StreamHandler()
app.logger.setLevel(logging.DEBUG)  # set the desired logging level here
app.logger.addHandler(file_handler)

@app.route('/')
def hello():
    app.logger.debug('A value for debugging')
    app.logger.warning('A value for warning')
    return 'Hello World!'

Alternatively, if you prefer you can do none of this and just enable debug mode for the Foreman/Heroku controlled application, though this would not be something I'd recommend for a production app:

from flask import Flask
app = Flask(__name__)
app.debug = True
like image 66
Miguel Avatar answered Oct 17 '22 06:10

Miguel