Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Logging - Debug setting

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?

like image 353
Giannis Avatar asked Apr 25 '15 19:04

Giannis


People also ask

How do I set debug mode in Flask?

If you're using the app. run() method instead of the flask run command, pass debug=True to enable debug mode. Tracebacks are also printed to the terminal running the server, regardless of development mode.

How do you set up a Flask logger?

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 set debug mode in Python?

Within a Python script To start the debugger within a script, use set_trace(). set_trace() is a Python function, therefore you can call it at any point in your program.


1 Answers

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

like image 79
Giannis Avatar answered Oct 07 '22 01:10

Giannis