5

I am using the following config for my Flask app:

class StagingConfig(Config):
    DEBUG = False
    MONGO_DB_NAME = "res_stage_database"

    @classmethod
    def init_app(cls, app):
        import logging
        from logging.handlers import RotatingFileHandler
        rotating_handler = RotatingFileHandler(filename='gunicorn.out', maxBytes=10000000, backupCount=5)
        rotating_handler.setLevel(logging.INFO)
        formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
        rotating_handler.setFormatter(formatter)
        app.logger.addHandler(rotating_handler)
        app.logger.info("Using StagingConfig")
        app.logger.error("Using StagingConfig")

(Above, appends only the error message to gunicorn.out- 2015-04-26 18:03:38,182 - ERROR - Using StagingConfig )

Since this config is used in a staged app, I want DEBUG to be False, so that I dont get Flask debug screens in case of errors, and instead the standard error 500 screen. Although for some reason, when DEBUG is set to False, logging of error messages except error stops.

As soon as DEBUG is set to True, logging occurs correctly. Shouldn't these values be independent, since I set my log level on the logging handler?

1 Answer 1

6

Turns out I had to use app.logger.setLevel(logging.INFO), instead of setting the level on the handler.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - this just ended a very frustrating time trying to get logging working in a flask app while following the official instructions.
On Heroku, unfortunately, that did not re-enable logging (using Flask and gunicorn). What ended up working was the code snippet posted here. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.