Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Flask: How to access app Logger within Blueprint

What is the standard way for a blueprint to access the application logger?

like image 487
Gal Bracha Avatar asked Jun 07 '13 23:06

Gal Bracha


People also ask

How do you use a logger in a Flask?

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 run a Flask app with blueprints?

To use any Flask Blueprint, you have to import it and then register it in the application using register_blueprint() .


2 Answers

inside the blueprint add:

from flask import current_app 

and when needed call:

current_app.logger.info('grolsh') 
like image 167
Gal Bracha Avatar answered Nov 03 '22 01:11

Gal Bracha


Btw, I use this pattern:

# core.py from werkzeug.local import LocalProxy from flask import current_app  logger = LocalProxy(lambda: current_app.logger)   # views.py from core import logger  @mod.route("/") def index():     logger.info("serving index")     ... 
like image 20
suzanshakya Avatar answered Nov 03 '22 00:11

suzanshakya