Skip to content

Commit 298bd06

Browse files
committed
set root log level from zappa log_level
fix isort finding in tests
1 parent b00312d commit 298bd06

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

tests/test_wsgi_root_log_level.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import logging
2+
3+
import pytest
4+
5+
from zappa.handler import LambdaHandler
6+
7+
"""
8+
https://github.com/zappa/Zappa/issues/1336
9+
2024-08-13 @ceturc
10+
Test that the root logger's level is updated when log_level is set.
11+
This was Zappa's default behavior prior to 0.59.0. This test is
12+
designed to prevent future regressions of logging.
13+
"""
14+
15+
event = {
16+
"body": "",
17+
"resource": "/{proxy+}",
18+
"requestContext": {},
19+
"queryStringParameters": {},
20+
"headers": {
21+
"Host": "example.com",
22+
},
23+
"pathParameters": {"proxy": "root-logger"},
24+
"httpMethod": "GET",
25+
"stageVariables": {},
26+
"path": "/root-logger",
27+
}
28+
29+
30+
@pytest.fixture()
31+
def reset_handler_singleton():
32+
"""
33+
Since the LambdaHandler is a singleton, it must be
34+
destroyed before tests for logging changes to take effect.
35+
"""
36+
LambdaHandler._LambdaHandler__instance = None
37+
yield
38+
39+
40+
def test_wsgi_root_log_level_debug(caplog, reset_handler_singleton):
41+
lh = LambdaHandler("tests.test_wsgi_root_log_level_settings_debug")
42+
response = lh.handler(event, None)
43+
assert response["statusCode"] == 200
44+
assert ("root", logging.DEBUG, "debug message") in caplog.record_tuples
45+
assert ("root", logging.INFO, "info message") in caplog.record_tuples
46+
assert ("root", logging.WARNING, "warning message") in caplog.record_tuples
47+
assert ("root", logging.ERROR, "error message") in caplog.record_tuples
48+
assert ("root", logging.CRITICAL, "critical message") in caplog.record_tuples
49+
50+
51+
def test_wsgi_root_log_level_info(caplog, reset_handler_singleton):
52+
lh = LambdaHandler("tests.test_wsgi_root_log_level_settings_info")
53+
response = lh.handler(event, None)
54+
assert response["statusCode"] == 200
55+
assert ("root", logging.DEBUG, "debug message") not in caplog.record_tuples
56+
assert ("root", logging.INFO, "info message") in caplog.record_tuples
57+
assert ("root", logging.WARNING, "warning message") in caplog.record_tuples
58+
assert ("root", logging.ERROR, "error message") in caplog.record_tuples
59+
assert ("root", logging.CRITICAL, "critical message") in caplog.record_tuples

tests/test_wsgi_root_log_level_app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
3+
from flask import Flask, request
4+
5+
app = Flask(__name__)
6+
7+
8+
@app.route("/root-logger", methods=["GET", "POST"])
9+
def return_request_url():
10+
logging.debug("debug message")
11+
logging.info("info message")
12+
logging.warning("warning message")
13+
logging.error("error message")
14+
logging.critical("critical message")
15+
return ""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
API_STAGE = "dev"
2+
APP_FUNCTION = "app"
3+
APP_MODULE = "tests.test_wsgi_root_log_level_app"
4+
BINARY_SUPPORT = False
5+
CONTEXT_HEADER_MAPPINGS = {}
6+
DEBUG = "True"
7+
DJANGO_SETTINGS = None
8+
DOMAIN = "api.example.com"
9+
ENVIRONMENT_VARIABLES = {}
10+
LOG_LEVEL = "DEBUG"
11+
PROJECT_NAME = "wsgi_root_log_level"
12+
COGNITO_TRIGGER_MAPPING = {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
API_STAGE = "dev"
2+
APP_FUNCTION = "app"
3+
APP_MODULE = "tests.test_wsgi_root_log_level_app"
4+
BINARY_SUPPORT = False
5+
CONTEXT_HEADER_MAPPINGS = {}
6+
DEBUG = "True"
7+
DJANGO_SETTINGS = None
8+
DOMAIN = "api.example.com"
9+
ENVIRONMENT_VARIABLES = {}
10+
LOG_LEVEL = "INFO"
11+
PROJECT_NAME = "wsgi_root_log_level"
12+
COGNITO_TRIGGER_MAPPING = {}

zappa/handler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def __init__(self, settings_name="zappa_settings", session=None):
7070
if self.settings.LOG_LEVEL:
7171
level = logging.getLevelName(self.settings.LOG_LEVEL)
7272
logger.setLevel(level)
73+
# https://github.com/zappa/Zappa/issues/1336
74+
# @ceturc 2024-08-13
75+
# Backwards compatibility to set root logger level after 0.59.0
76+
root_logger = logging.getLogger()
77+
root_logger.setLevel(level)
7378

7479
remote_env = getattr(self.settings, "REMOTE_ENV", None)
7580
remote_bucket, remote_file = parse_s3_url(remote_env)

0 commit comments

Comments
 (0)