Skip to content

Instantly share code, notes, and snippets.

@ibeex
Created August 4, 2012 13:46
Show Gist options
  • Save ibeex/3257877 to your computer and use it in GitHub Desktop.
Save ibeex/3257877 to your computer and use it in GitHub Desktop.
Flask logging example
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()
@ravishankarachanta
Copy link

thanks much

@ravishankarachanta
Copy link

ravishankarachanta commented Aug 12, 2019

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:

https://www.packetmischief.ca/2017/10/25/3-ways-to-fail-at-logging-with-flask/?unapproved=27590&moderation-hash=51a1d5344643c9a0f5d47a253641ac53

I'm not sure if the issue is with me unable to get it working or this happens really

@SchopmanBente
Copy link

Thanks!It really helps me!

@UnSstrennen
Copy link

Thanks a lot!

@mateo2181
Copy link

mateo2181 commented Jun 3, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment