Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the API logger in Flask-Restful's resources

Tags:

python

rest

flask

I'm currently using flask-restful (http://flask-restful.readthedocs.io/en/0.3.5/index.html) to deploy resources as endpoints and I'm wondering if there's a way to access the API logger from within then resources classes. I've skimmed through the docs and couldn't find the appropriate answer.

Basically I want to do that :

from flask_restful import Resource 

class SomeEndpoint(Resource):

    def get(self):

        try:

            ... something throws an exception

        except SomeException as se:

            ... send custom message to API logger         <----- Here!

        return response

What I though of doing was passing the logger from the API through the constructor of the Resource like that :

App = Flask(__name__)
api = Api(App)
api.add_resource(SomeEndpoint, '/', resource_class_kwargs={'logger': App.logger})

Is this the most appropriate way to access the logger inside flask-restful resource endpoints ?

Thanks a lot

like image 892
ElCapitaine Avatar asked Mar 01 '18 15:03

ElCapitaine


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.

Where are logs stored in Flask?

Using the Default Logging System for Flask Flask uses the Python logging system itself to trace out events during the application's run-time. Python Logging has a default Logger – BasicConfig which we can use to log our messages. The logs are stored in files with . log extension.

How do I hit a Flask API?

When we run the above file using POSTMAN, we try to get the data without login to give you unauthorized access. Now go to authorization and click on Basic Authorization. Enter the username and password you have used and then hit GET request to get the desired result. This is how you can secure your Flask API.


2 Answers

I know the answer has been chosen already, but there is a slightly different approach that also works.

First, import

from flask import current_app as app

in the resource file, and when calling the logger, do:

app.logger.info("This is an info message")

like image 63
Campello Avatar answered Oct 21 '22 13:10

Campello


You need to define constructor of Resource. Here an example:

import logging


class SomeEndpoint(Resource):

    def __init__(self, **kwargs):
        self.logger = kwargs.get('logger')

    def get(self):
        # self.logger - 'logger' from resource_class_kwargs
        return self.logger.name  

api.add_resource(SomeEndpoint, '/', resource_class_kwargs={
    # any logger here...
    'logger': logging.getLogger('my_custom_logger')
})

Open your endpoint. You will see my_custom_logger. Hope this helps.

like image 22
Danila Ganchar Avatar answered Oct 21 '22 14:10

Danila Ganchar