diff --git a/tests/test_wsgi_root_log_level.py b/tests/test_wsgi_root_log_level.py new file mode 100644 index 000000000..9e5ddbb70 --- /dev/null +++ b/tests/test_wsgi_root_log_level.py @@ -0,0 +1,57 @@ +import pytest +import logging +from zappa.handler import LambdaHandler + +""" +https://github.com/zappa/Zappa/issues/1336 +2024-08-13 @ceturc +Test that the root logger's level is updated when log_level is set. +This was Zappa's default behavior prior to 0.59.0. This test is +designed to prevent future regressions of logging. +""" + +event = { + "body": "", + "resource": "/{proxy+}", + "requestContext": {}, + "queryStringParameters": {}, + "headers": { + "Host": "example.com", + }, + "pathParameters": {"proxy": "root-logger"}, + "httpMethod": "GET", + "stageVariables": {}, + "path": "/root-logger", +} + + +@pytest.fixture() +def reset_handler_singleton(): + """ + Since the LambdaHandler is a singleton, it must be + destroyed before tests for logging changes to take effect. + """ + LambdaHandler._LambdaHandler__instance = None + yield + + +def test_wsgi_root_log_level_debug(caplog, reset_handler_singleton): + lh = LambdaHandler("tests.test_wsgi_root_log_level_settings_debug") + response = lh.handler(event, None) + assert response["statusCode"] == 200 + assert ("root", logging.DEBUG, "debug message") in caplog.record_tuples + assert ("root", logging.INFO, "info message") in caplog.record_tuples + assert ("root", logging.WARNING, "warning message") in caplog.record_tuples + assert ("root", logging.ERROR, "error message") in caplog.record_tuples + assert ("root", logging.CRITICAL, "critical message") in caplog.record_tuples + + +def test_wsgi_root_log_level_info(caplog, reset_handler_singleton): + lh = LambdaHandler("tests.test_wsgi_root_log_level_settings_info") + response = lh.handler(event, None) + assert response["statusCode"] == 200 + assert ("root", logging.DEBUG, "debug message") not in caplog.record_tuples + assert ("root", logging.INFO, "info message") in caplog.record_tuples + assert ("root", logging.WARNING, "warning message") in caplog.record_tuples + assert ("root", logging.ERROR, "error message") in caplog.record_tuples + assert ("root", logging.CRITICAL, "critical message") in caplog.record_tuples diff --git a/tests/test_wsgi_root_log_level_app.py b/tests/test_wsgi_root_log_level_app.py new file mode 100644 index 000000000..a01d787ca --- /dev/null +++ b/tests/test_wsgi_root_log_level_app.py @@ -0,0 +1,14 @@ +from flask import Flask, request +import logging + +app = Flask(__name__) + + +@app.route("/root-logger", methods=["GET", "POST"]) +def return_request_url(): + logging.debug("debug message") + logging.info("info message") + logging.warning("warning message") + logging.error("error message") + logging.critical("critical message") + return "" diff --git a/tests/test_wsgi_root_log_level_settings_debug.py b/tests/test_wsgi_root_log_level_settings_debug.py new file mode 100644 index 000000000..be9444502 --- /dev/null +++ b/tests/test_wsgi_root_log_level_settings_debug.py @@ -0,0 +1,12 @@ +API_STAGE = "dev" +APP_FUNCTION = "app" +APP_MODULE = "tests.test_wsgi_root_log_level_app" +BINARY_SUPPORT = False +CONTEXT_HEADER_MAPPINGS = {} +DEBUG = "True" +DJANGO_SETTINGS = None +DOMAIN = "api.example.com" +ENVIRONMENT_VARIABLES = {} +LOG_LEVEL = "DEBUG" +PROJECT_NAME = "wsgi_root_log_level" +COGNITO_TRIGGER_MAPPING = {} diff --git a/tests/test_wsgi_root_log_level_settings_info.py b/tests/test_wsgi_root_log_level_settings_info.py new file mode 100644 index 000000000..77bae8f82 --- /dev/null +++ b/tests/test_wsgi_root_log_level_settings_info.py @@ -0,0 +1,12 @@ +API_STAGE = "dev" +APP_FUNCTION = "app" +APP_MODULE = "tests.test_wsgi_root_log_level_app" +BINARY_SUPPORT = False +CONTEXT_HEADER_MAPPINGS = {} +DEBUG = "True" +DJANGO_SETTINGS = None +DOMAIN = "api.example.com" +ENVIRONMENT_VARIABLES = {} +LOG_LEVEL = "INFO" +PROJECT_NAME = "wsgi_root_log_level" +COGNITO_TRIGGER_MAPPING = {} diff --git a/zappa/handler.py b/zappa/handler.py index 7287e09f7..47ce32d5a 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -70,6 +70,11 @@ def __init__(self, settings_name="zappa_settings", session=None): if self.settings.LOG_LEVEL: level = logging.getLevelName(self.settings.LOG_LEVEL) logger.setLevel(level) + # https://github.com/zappa/Zappa/issues/1336 + # @ceturc 2024-08-13 + # Backwards compatibility to set root logger level after 0.59.0 + root_logger = logging.getLogger() + root_logger.setLevel(level) remote_env = getattr(self.settings, "REMOTE_ENV", None) remote_bucket, remote_file = parse_s3_url(remote_env)