Don Smythe
Don Smythe

Reputation: 9814

Flask logging configuration issue

I am trying to configure multiple loggers for a flask app.

applogger.py:

import logging
from logging.handlers import SMTPHandler, RotatingFileHandler 

def setup_logging(app):
    file_handler = RotatingFileHandler("logger_out.txt")
    email_handler = SMTPHandler(...)
    loggers = [app.logger]
    for logger in loggers:
        logger.addHandler(email_handler)
        logger.addHandler(file_handler)
logger = LocalProxy(lambda: current_app.logger)

appsetup.py

def create_app(object_name):
    app = Flask(__name__)
    app.config.from_object(object_name)
    setup_logging(app)

appview.py

from applogger import logger

@app_view.route('/')
def index():
    logger.debug(">>index")
    return render_template('home.html')

Logging to the console works but the file and email loggers are not working. Also it looks like a bit of a hack passing the app into the setup_logging() method in the applogger.py module. Prior to this I was using

app = current_app._get_current_object() 

from within the setup_logging() method, but got a 'working outside of application context' error. Any ideas on a recommended way to configure multiple loggers for Flask?

Upvotes: 2

Views: 773

Answers (2)

Don Smythe
Don Smythe

Reputation: 9814

I found that the logger output file location was one level up from where I expected. Using ../log_out.txt in the logger file put the logger file two directories up at the level of the application folder.

Upvotes: 1

sebenalern
sebenalern

Reputation: 2559

If Debug = True than flask will only log something consider as an ERROR try setting your log level like this:

app.logger.setLevel(logging.DEBUG)

Upvotes: 1

Related Questions