Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading config value in controller #13

Open
zrehman-ssi opened this issue Sep 23, 2019 · 3 comments
Open

Reading config value in controller #13

zrehman-ssi opened this issue Sep 23, 2019 · 3 comments

Comments

@zrehman-ssi
Copy link

zrehman-ssi commented Sep 23, 2019

Hi,

First of all thank you very much for such a great boilerplate. I'm facing an issue that i would like to know your thoughts about.

I've created a logging service in Service package which is in main package. Here is the content of loggingservice.

import logging 
from datetime import datetime
from logging.handlers import TimedRotatingFileHandler
import app

def get_logger(name):
    log_format = app.config['LOGGING_MESSAGE_FORMAT']
    time_rotating_file_handler = TimedRotatingFileHandler("logs/SIB.log", when="midnight", interval=1)
    time_rotating_file_handler.setLevel(logging.DEBUG)
    time_rotating_file_handler.setFormatter(logging.Formatter(log_format))
    logging.getLogger(name).addHandler(time_rotating_file_handler)
    return logging.getLogger(name)

Now i want to get_logger in controller. Here is how i've used it in auth_controller

from ..service.logging_service import get_logger

_logger = get_logger(__name__)

@api.route('/login')
class Login(Resource):
    @api.doc('user_login')
    @api.expect(_login, validate=True)
    def post(self):
        _logger.error("Logging Test")

For brevity I've removed unwanted code from auth_controller.

Now the problem is that when i try to run application it tries to run auth_controller first, which crashes due the fact that we haven't initiate the config object on app. It is initiated when create_app is called from manage.py.

Can you kindly suggest what can be done to resolve this issue?

Regards

@fullonic
Copy link

Hey @zrehman-ssi , have you fixed your issue?

@zrehman-ssi
Copy link
Author

Hi @fullonic,
I've used the python configuration file for now. I haven't been able to see the way to use config file entries in any file that is going to be executed before initiating the configurations. If you have any suggestions, then kindly let me know.

@fullonic
Copy link

fullonic commented Nov 20, 2019

Hi @zrehman-ssi , from what do you explains and reading your code, I believe that your are running into "circular imports issues" because your are importing the app. When using the app factory, or the def create_app(): and you need to import your app in some module, flask provides from flask import current_app. So, doing that, get_logger() would be:

from flask import current_app

def get_logger(name):
    # use current_app instead of app
    log_format = current_app.config['LOGGING_MESSAGE_FORMAT']  
    (...)

For better explanation see it here.

Let me know if it fixes your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants