From f968f649f1940ae1b9ea87a54b24d617a6a1b5b2 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Wed, 8 Feb 2023 10:20:46 -0500 Subject: [PATCH 1/5] Add functionality to configure logging level Add the ability to configure the logging level for a given invocation of the Lambda. We use an environment variable to also highlight how a Lambda might be configured to run with a specific configuration. This environment variable will change the logging level for a given invocation of the Lambda and then reset it to the previous level. --- src/lambda_handler.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lambda_handler.py b/src/lambda_handler.py index e61ce6e..74ca692 100644 --- a/src/lambda_handler.py +++ b/src/lambda_handler.py @@ -3,6 +3,7 @@ # Standard Python Libraries from datetime import datetime, timezone import logging +import os from typing import Any, Optional, Union # Third-Party Libraries @@ -12,8 +13,9 @@ # cisagov Libraries from example import example_div +default_log_level = "INFO" logger = logging.getLogger() -logger.setLevel(logging.INFO) +logger.setLevel(default_log_level) def failed_task(result: dict[str, Any], error_msg: str) -> None: @@ -91,8 +93,22 @@ def handler(event, context) -> dict[str, Optional[str]]: :param context: The context in which the function is called. :return: The result of the action. """ + old_log_level = None response: dict[str, Optional[str]] = {"timestamp": str(datetime.now(timezone.utc))} + # Update the logging level if necessary + new_log_level = os.environ.get("log_level", default_log_level).upper() + if not isinstance(logging.getLevelName(new_log_level), int): + logging.warning( + "Invalid logging level %s. Using %s instead.", + new_log_level, + default_log_level, + ) + new_log_level = default_log_level + if logging.getLogger().getEffectiveLevel() != logging.getLevelName(new_log_level): + old_log_level = logging.getLogger().getEffectiveLevel() + logging.getLogger().setLevel(new_log_level) + task_name = f"task_{event.get('task')}" task = globals().get(task_name, task_default) @@ -103,6 +119,9 @@ def handler(event, context) -> dict[str, Optional[str]]: result = task_default(event) else: result = task(event) - response.update(result) + + if old_log_level is not None: + logging.getLogger().setLevel(old_log_level) + return response From bdb5202e8ef355d6dda7dc3c817120a49c93d2e1 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Wed, 8 Feb 2023 10:55:59 -0500 Subject: [PATCH 2/5] Add development requirement The `semver` package was not in the develop requirements even though it is required by the bump_version.sh script. --- requirements-dev.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements-dev.txt b/requirements-dev.txt index 8d26d98..bdc1615 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ --requirement requirements-test.txt ipython pipenv +semver From f23dfa157208c2c4ec7d1c749b62b5c54a40d296 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Wed, 8 Feb 2023 10:58:08 -0500 Subject: [PATCH 3/5] Bump version from 0.0.1 to 0.0.2 --- src/version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.txt b/src/version.txt index f102a9c..3b93d0b 100644 --- a/src/version.txt +++ b/src/version.txt @@ -1 +1 @@ -__version__ = "0.0.1" +__version__ = "0.0.2" From 16901db7074e866b4f066b9eaa1310a4bdd36537 Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Wed, 8 Feb 2023 10:58:22 -0500 Subject: [PATCH 4/5] Bump version from 0.0.2 to 0.0.2-rc.1 --- src/version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.txt b/src/version.txt index 3b93d0b..55fd21f 100644 --- a/src/version.txt +++ b/src/version.txt @@ -1 +1 @@ -__version__ = "0.0.2" +__version__ = "0.0.2-rc.1" From ccffb7079574b68bda703777390f423e5292827d Mon Sep 17 00:00:00 2001 From: Nicholas McDonnell <50747025+mcdonnnj@users.noreply.github.com> Date: Thu, 2 Mar 2023 12:06:58 -0500 Subject: [PATCH 5/5] Finalize version from 0.0.2-rc.1 to 0.0.2 --- src/version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.txt b/src/version.txt index 55fd21f..3b93d0b 100644 --- a/src/version.txt +++ b/src/version.txt @@ -1 +1 @@ -__version__ = "0.0.2-rc.1" +__version__ = "0.0.2"