From b5e4bdeddc4793d48a6a88468bfce09a5fb61dde Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: May 17 2018 03:06:00 +0000 Subject: set up logging separately from Flask app creation In the tests we could call create_app() zero or more times, which meant that log messages produced in the test process could be duplicated on stdout many times depending on which set of tests had been run. What we really want is: * logging configured and going to stdout in the WSGI app (as it is now); * logging configured with *no* handler in the test suite, so that pytest can capture the log messages. --- diff --git a/functional-tests/conftest.py b/functional-tests/conftest.py index 5d5c499..13d081b 100644 --- a/functional-tests/conftest.py +++ b/functional-tests/conftest.py @@ -13,6 +13,8 @@ import pytest import requests from sqlalchemy import create_engine +from greenwave.logger import init_logging + log = logging.getLogger(__name__) @@ -21,6 +23,13 @@ log = logging.getLogger(__name__) TEST_HTTP_TIMEOUT = int(os.environ.get('TEST_HTTP_TIMEOUT', 2)) +@pytest.fixture(scope='session', autouse=True) +def logging(): + init_logging() + # We don't configure any log handlers, let pytest capture the log + # messages and display them instead. + + def drop_and_create_database(dbname): """ Drops (if exists) and re-creates the given database on the local Postgres instance. diff --git a/greenwave/app_factory.py b/greenwave/app_factory.py index 01c807b..d9c2f0b 100644 --- a/greenwave/app_factory.py +++ b/greenwave/app_factory.py @@ -1,7 +1,6 @@ # SPDX-License-Identifier: GPL-2.0+ from flask import Flask -from greenwave.logger import init_logging from greenwave.api_v1 import api from greenwave.utils import json_error, load_config @@ -25,9 +24,6 @@ def create_app(config_obj=None): app.register_error_handler(ConnectionError, json_error) app.register_error_handler(Timeout, json_error) - # initialize logging - init_logging(app) - # register blueprints app.register_blueprint(api, url_prefix="/api/v1.0") app.add_url_rule('/healthcheck', view_func=healthcheck) diff --git a/greenwave/logger.py b/greenwave/logger.py index fc1b831..19ad982 100644 --- a/greenwave/logger.py +++ b/greenwave/logger.py @@ -13,9 +13,7 @@ def log_to_stdout(level=logging.INFO): logging.getLogger().addHandler(stream_handler) -def init_logging(app): - log_level = logging.DEBUG if app.debug else logging.INFO - log_to_stdout(level=log_level) +def init_logging(): # In general we want to see everything from our own code, # but not detailed debug messages from third-party libraries. # Note that the log level on the handler above controls what diff --git a/greenwave/wsgi.py b/greenwave/wsgi.py index b9957d5..c89f466 100644 --- a/greenwave/wsgi.py +++ b/greenwave/wsgi.py @@ -1,4 +1,9 @@ # SPDX-License-Identifier: GPL-2.0+ +import logging +from greenwave.logger import init_logging, log_to_stdout from greenwave.app_factory import create_app + app = create_app() +init_logging() +log_to_stdout(level=logging.DEBUG if app.debug else logging.INFO) diff --git a/run-dev-server.py b/run-dev-server.py index b963378..021b080 100644 --- a/run-dev-server.py +++ b/run-dev-server.py @@ -2,10 +2,15 @@ # SPDX-License-Identifier: GPL-2.0+ +import logging + from greenwave.app_factory import create_app +from greenwave.logger import init_logging, log_to_stdout if __name__ == '__main__': app = create_app('greenwave.config.DevelopmentConfig') + init_logging() + log_to_stdout(level=logging.DEBUG) app.run( host=app.config['HOST'], port=app.config['PORT'],