Search code examples
pythonpython-3.xloggingflaskapache-superset

How to configure logging for an imported module?


In my Superset web application, I am interested in setting the level of logging in Flask-OAuthLib to DEBUG. We can see Flask-OAuthLib access its logger here on line 26 from a Superset web application.

Superset is a web application implemented using Flask-AppBuilder. It allows for OAuth2 authentication via Flask-OAuthLib.

I want to configure Flask-OAuthLib logging from custom_sso_security_manager.py ... a module described in the Superset docs on custom OAuth configuration.


Solution

  • You can access the logger in exactly the same way. They are added to a global dictionary that items can be get from with getLogger(key). So all you need is put something like this into your file after you imported the oauth lib:

    oauth_logger = logging.getLogger('flask_oauthlib')
    oauth_logger.setLevel(logging.DEBUG)
    
    # it is custom for libs to have no handler (except the NullHandler)
    # so you may want to add one:
    oauth_logger.addHandler(logging.StreamHandler()) # just an example