Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set root log level from zappa log_level #1347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions tests/test_wsgi_root_log_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging

import pytest

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
15 changes: 15 additions & 0 deletions tests/test_wsgi_root_log_level_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

from flask import Flask, request

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