Skip to content

Commit

Permalink
set root log level from zappa log_level
Browse files Browse the repository at this point in the history
  • Loading branch information
ceturc committed Aug 13, 2024
1 parent 39f75e7 commit f16bc6b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/test_wsgi_root_log_level.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tests/test_wsgi_root_log_level_app.py
Original file line number Diff line number Diff line change
@@ -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 ""
12 changes: 12 additions & 0 deletions tests/test_wsgi_root_log_level_settings_debug.py
Original file line number Diff line number Diff line change
@@ -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 = {}
12 changes: 12 additions & 0 deletions tests/test_wsgi_root_log_level_settings_info.py
Original file line number Diff line number Diff line change
@@ -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 = {}
5 changes: 5 additions & 0 deletions zappa/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f16bc6b

Please sign in to comment.