-
-
Save ibeex/3257877 to your computer and use it in GitHub Desktop.
A warning occurred (42 apples) | |
An error occurred |
import logging | |
from logging.handlers import RotatingFileHandler | |
from flask import Flask | |
app = Flask(__name__) | |
@app.route('/') | |
def foo(): | |
app.logger.warning('A warning occurred (%d apples)', 42) | |
app.logger.error('An error occurred') | |
app.logger.info('Info') | |
return "foo" | |
if __name__ == '__main__': | |
handler = RotatingFileHandler('foo.log', maxBytes=10000, backupCount=1) | |
handler.setLevel(logging.INFO) | |
app.logger.addHandler(handler) | |
app.run() |
This gist seems to print 4 lines for every call to log, e.g.
--------------------------------------------------------------------------------
INFO in app [/usr/src/app/app.py:187]:
successful query
--------------------------------------------------------------------------------
How do you get it to print on just one line? http://stackoverflow.com/questions/41783774/how-to-log-in-flask
Hi All,
I am using logging in various python apps, your post logging for flask speciically was helpful, however is there a web based log viewer, that I can just login and view logs if any generated by my app ?
Thanks a lot
Thanks.
Thinks
Very useful, thank you! Some other logging handlers are here: https://docs.python.org/2/library/logging.handlers.html
Very helpful, thank you!
Thank you for this gist. In addition if you only want to log to stdout do the following:
import sys
import logging
import flask
app = flask.Flask(__name__)
@app.before_first_request
def setup_logging():
if not app.debug:
# In production mode, add log handler to sys.stdout.
app.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
app.logger.setLevel(logging.INFO)
This is necessary, because Flask does not log messages in production mode by default. To get Flask's app.logger to log messages, you'll always have to add a logging handler, even for simply logging to stdout.
see
It is not the case (at least anymore) that you'll always have to explicitly add a logging handler to get Flask's app.logger to log messages. According to http://flask.pocoo.org/docs/1.0/logging/, we can simply make a logging.basicConfig(...)
call before creating the Flask app, like
import flask
import logging
logging.basicConfig(filename=LOG_FILENAME, format=LOG_FORMAT, level=LOG_LEVEL)
app = Flask(__name__)
This worked for me in a production environment on all log levels.
thanks much
I was using TimedRotatingFileHandler in my Flask app under Gunicorn with multiple workers. I ended up seeing logs from same time being written into different files (i.e one of the workers seem to be writing to a rotated file). Was trying to look thorough if thats the case with others and I see this Blog post:
I'm not sure if the issue is with me unable to get it working or this happens really
Thanks!It really helps me!
Thanks a lot!
is there a way to log errors (ex: timeout connection DB) inside gunicorn logs? Or how should I log errors in a flask app running in production with Gunicorn?
you can try this its very easy : https://github.com/ranvijay-sachan/flask-core-python-logging