Flask logging with Foreman

Yarin

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
Miguel

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Disable Asset logging in Rails 4 + unicorn + foreman

From Dev

Flask logging not working at all

From Dev

Flask logging configuration issue

From Dev

Sentry logging via Flask app

From Dev

Flask: expire token on logging out

From Dev

Flask - Logging after response flushed

From Dev

Flask - Logging after response flushed

From Dev

Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

From Dev

Docker with foreman

From Dev

Disable Flask-SocketIO logging to terminal

From Dev

automatically logging flask's messages to a logger

From Dev

Logging out a specific user with Flask-Login

From Dev

Getting request context for error logging in Flask?

From Dev

Python Flask - tracking a request for logging purposes

From Dev

Where to put logging setup code in a flask app?

From Dev

Multiple requests Flask, Gunicorn , Nginx logging not working

From Dev

How to enrich logging messages with request information in flask?

From Dev

logging is not working under Flask in Python 3

From Dev

Background thread logging to a Server Sent Event stream in a Flask app

From Dev

Logging in with Facebook using Flask-Stormpath on PythonAnywhere raises JSONDecodeError

From Dev

uwsgi + flask logging.config not working and also breaks application

From Dev

Killing Processes Spawned by Foreman

From Dev

Is there a way to use figaro with foreman?

From Dev

Foreman exiting with code 1

From Dev

Foreman: Command "start" is not defined

From Dev

Foreman start multiple processes?

From Dev

Foreman rvm upstart not work

From Dev

Heroku, Django, Foreman

From Dev

foreman: command not found

Related Related

HotTag

Archive