Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
seshubaws authored Nov 21, 2023
2 parents ff8d490 + 19f5641 commit e87b05a
Show file tree
Hide file tree
Showing 40 changed files with 665 additions and 303 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/label_pr_on_title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: "Label PR based on title"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
PR_TITLE: ${{ needs.get_pr_details.outputs.prTitle }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on_label_added.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
# Maintenance: Persist state per PR as an artifact to avoid spam on label add
- name: "Suggest split large Pull Request"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
PR_ACTION: ${{ needs.get_pr_details.outputs.prAction }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on_merged_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: "Label PR related issue for release"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/on_opened_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: "Ensure related issue is present"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
Expand All @@ -68,7 +68,7 @@ jobs:
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: "Ensure acknowledgement section is present"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
Expand Down
10 changes: 1 addition & 9 deletions .github/workflows/quality_check_pydanticv2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,5 @@ jobs:
run: poetry add "pydantic=^2.0.3"
- name: Install dependencies
run: make dev
- name: Formatting and Linting
run: make lint
- name: Static type checking
run: make mypy
- name: Test with pytest
run: make test
- name: Security baseline
run: make security-baseline
- name: Complexity baseline
run: make complexity-baseline
run: make test-pydanticv2
2 changes: 1 addition & 1 deletion .github/workflows/record_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: "Extract PR details"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const script = require('.github/scripts/save_pr_details.js')
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ jobs:
artifact_name: ${{ needs.seal.outputs.artifact_name }}

- name: Close issues related to this release
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable_export_pr_details.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- name: Checkout repository # in case caller workflow doesn't checkout thus failing with file not found
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: "Download previously saved PR"
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
WORKFLOW_ID: ${{ inputs.record_pr_workflow_id }}
# For security, we only download artifacts tied to the successful PR recording workflow
Expand Down
45 changes: 44 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,50 @@
<a name="unreleased"></a>
# Unreleased

## Documentation

* **logger:** improve ALC messaging in the PT context ([#3359](https://github.com/aws-powertools/powertools-lambda-python/issues/3359))
* **logger:** Fix ALC link ([#3352](https://github.com/aws-powertools/powertools-lambda-python/issues/3352))

## Maintenance

* **ci:** lint and type checking removal in Pydantic v2 quality check ([#3360](https://github.com/aws-powertools/powertools-lambda-python/issues/3360))
* **deps:** bump squidfunk/mkdocs-material from `f486dc9` to `2c57e4d` in /docs ([#3366](https://github.com/aws-powertools/powertools-lambda-python/issues/3366))
* **deps:** bump the layer-balancer group in /layer/scripts/layer-balancer with 2 updates ([#3353](https://github.com/aws-powertools/powertools-lambda-python/issues/3353))
* **deps-dev:** bump aws-cdk from 2.109.0 to 2.110.0 ([#3361](https://github.com/aws-powertools/powertools-lambda-python/issues/3361))
* **deps-dev:** bump the boto-typing group with 11 updates ([#3362](https://github.com/aws-powertools/powertools-lambda-python/issues/3362))
* **deps-dev:** bump aws-cdk-lib from 2.108.1 to 2.110.0 ([#3365](https://github.com/aws-powertools/powertools-lambda-python/issues/3365))
* **deps-dev:** bump aws-cdk from 2.108.1 to 2.109.0 ([#3354](https://github.com/aws-powertools/powertools-lambda-python/issues/3354))
* **deps-dev:** bump cfn-lint from 0.83.2 to 0.83.3 ([#3363](https://github.com/aws-powertools/powertools-lambda-python/issues/3363))
* **deps-dev:** bump ruff from 0.1.5 to 0.1.6 ([#3364](https://github.com/aws-powertools/powertools-lambda-python/issues/3364))


<a name="v2.27.0"></a>
## [v2.27.0] - 2023-11-16
## Features

* **logger:** Adding support to new env variables ([#3348](https://github.com/aws-powertools/powertools-lambda-python/issues/3348))

## Maintenance

* version bump
* **deps:** bump actions/github-script from 6.4.1 to 7.0.0 ([#3330](https://github.com/aws-powertools/powertools-lambda-python/issues/3330))
* **deps:** bump the layer-balancer group in /layer/scripts/layer-balancer with 2 updates ([#3340](https://github.com/aws-powertools/powertools-lambda-python/issues/3340))
* **deps:** bump fastjsonschema from 2.18.1 to 2.19.0 ([#3337](https://github.com/aws-powertools/powertools-lambda-python/issues/3337))
* **deps:** bump the layer-balancer group in /layer/scripts/layer-balancer with 3 updates ([#3345](https://github.com/aws-powertools/powertools-lambda-python/issues/3345))
* **deps:** bump actions/dependency-review-action from 3.1.2 to 3.1.3 ([#3331](https://github.com/aws-powertools/powertools-lambda-python/issues/3331))
* **deps:** bump the layer-balancer group in /layer/scripts/layer-balancer with 1 update ([#3329](https://github.com/aws-powertools/powertools-lambda-python/issues/3329))
* **deps:** bump datadog-lambda from 4.81.0 to 4.82.0 ([#3338](https://github.com/aws-powertools/powertools-lambda-python/issues/3338))
* **deps-dev:** bump cfn-lint from 0.83.1 to 0.83.2 ([#3335](https://github.com/aws-powertools/powertools-lambda-python/issues/3335))
* **deps-dev:** bump aws-cdk from 2.108.0 to 2.108.1 ([#3344](https://github.com/aws-powertools/powertools-lambda-python/issues/3344))
* **deps-dev:** bump sentry-sdk from 1.34.0 to 1.35.0 ([#3334](https://github.com/aws-powertools/powertools-lambda-python/issues/3334))
* **deps-dev:** bump pytest-xdist from 3.3.1 to 3.4.0 ([#3332](https://github.com/aws-powertools/powertools-lambda-python/issues/3332))
* **deps-dev:** bump aws-cdk-lib from 2.107.0 to 2.108.1 ([#3343](https://github.com/aws-powertools/powertools-lambda-python/issues/3343))
* **deps-dev:** bump aws-cdk from 2.106.0 to 2.106.1 ([#3328](https://github.com/aws-powertools/powertools-lambda-python/issues/3328))
* **deps-dev:** bump aws-cdk-lib from 2.105.0 to 2.106.0 ([#3319](https://github.com/aws-powertools/powertools-lambda-python/issues/3319))
* **deps-dev:** bump aws-cdk from 2.105.0 to 2.106.0 ([#3320](https://github.com/aws-powertools/powertools-lambda-python/issues/3320))
* **deps-dev:** bump aws-cdk from 2.106.1 to 2.108.0 ([#3341](https://github.com/aws-powertools/powertools-lambda-python/issues/3341))
* **deps-dev:** bump aws-cdk-lib from 2.106.0 to 2.107.0 ([#3333](https://github.com/aws-powertools/powertools-lambda-python/issues/3333))


<a name="v2.26.1"></a>
Expand Down Expand Up @@ -3981,7 +4023,8 @@
* Merge pull request [#5](https://github.com/aws-powertools/powertools-lambda-python/issues/5) from jfuss/feat/python38


[Unreleased]: https://github.com/aws-powertools/powertools-lambda-python/compare/v2.26.1...HEAD
[Unreleased]: https://github.com/aws-powertools/powertools-lambda-python/compare/v2.27.0...HEAD
[v2.27.0]: https://github.com/aws-powertools/powertools-lambda-python/compare/v2.26.1...v2.27.0
[v2.26.1]: https://github.com/aws-powertools/powertools-lambda-python/compare/v2.26.0...v2.26.1
[v2.26.0]: https://github.com/aws-powertools/powertools-lambda-python/compare/v2.25.1...v2.26.0
[v2.25.1]: https://github.com/aws-powertools/powertools-lambda-python/compare/v2.25.0...v2.25.1
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ test:
poetry run pytest -m "not perf" --ignore tests/e2e --cov=aws_lambda_powertools --cov-report=xml
poetry run pytest --cache-clear tests/performance

test-pydanticv2:
poetry run pytest -m "not perf" --ignore tests/e2e

unit-test:
poetry run pytest tests/unit

Expand Down
115 changes: 97 additions & 18 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import random
import sys
import warnings
from typing import (
IO,
TYPE_CHECKING,
Expand All @@ -21,16 +22,15 @@
overload,
)

import jmespath

from aws_lambda_powertools.logging import compat

from ..shared import constants
from ..shared.functions import (
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import (
extract_event_from_common_models,
resolve_env_var_choice,
resolve_truthy_env_var_choice,
)
from aws_lambda_powertools.utilities import jmespath_utils

from ..shared.types import AnyCallableT
from .exceptions import InvalidLoggerSamplingRateError
from .filters import SuppressFilter
Expand Down Expand Up @@ -76,7 +76,7 @@ class Logger:
---------------------
POWERTOOLS_SERVICE_NAME : str
service name
LOG_LEVEL: str
POWERTOOLS_LOG_LEVEL: str
logging level (e.g. INFO, DEBUG)
POWERTOOLS_LOGGER_SAMPLE_RATE: float
sampling rate ranging from 0 to 1, 1 being 100% sampling
Expand Down Expand Up @@ -297,7 +297,7 @@ def _init_logger(
if self.child or is_logger_preconfigured:
return

self.setLevel(self._determine_log_level(log_level))
self.setLevel(log_level)
self._configure_sampling()
self.addHandler(self.logger_handler)
self.structure_logs(formatter_options=formatter_options, **kwargs)
Expand Down Expand Up @@ -442,7 +442,9 @@ def decorate(event, context, *args, **kwargs):
self.append_keys(cold_start=cold_start, **lambda_context.__dict__)

if correlation_id_path:
self.set_correlation_id(jmespath.search(correlation_id_path, event))
self.set_correlation_id(
jmespath_utils.extract_data_from_envelope(envelope=correlation_id_path, data=event),
)

if log_event:
logger.debug("Event received")
Expand Down Expand Up @@ -506,7 +508,7 @@ def exception(
self,
msg: object,
*args,
exc_info=True,
exc_info: logging._ExcInfoType = True,
stack_info: bool = False,
stacklevel: int = 2,
extra: Optional[Mapping[str, object]] = None,
Expand Down Expand Up @@ -676,12 +678,18 @@ def get_correlation_id(self) -> Optional[str]:
return self.registered_formatter.log_format.get("correlation_id")
return None

def setLevel(self, level: Union[str, int]) -> None:
return self._logger.setLevel(level)
def setLevel(self, level: Union[str, int, None]) -> None:
return self._logger.setLevel(self._determine_log_level(level))

def addHandler(self, handler: logging.Handler) -> None:
return self._logger.addHandler(handler)

def addFilter(self, filter: logging._FilterType) -> None: # noqa: A002 # filter built-in usage
return self._logger.addFilter(filter)

def removeFilter(self, filter: logging._FilterType) -> None: # noqa: A002 # filter built-in usage
return self._logger.removeFilter(filter)

@property
def registered_handler(self) -> logging.Handler:
"""Convenience property to access the first logger handler"""
Expand Down Expand Up @@ -714,17 +722,88 @@ def handlers(self) -> List[logging.Handler]:
"""
return self._logger.handlers

@staticmethod
def _determine_log_level(level: Union[str, int, None]) -> Union[str, int]:
"""Returns preferred log level set by the customer in upper case"""
def _get_aws_lambda_log_level(self) -> Optional[str]:
"""
Retrieve the log level for AWS Lambda from the Advanced Logging Controls feature.
Returns:
Optional[str]: The corresponding logging level.
"""

return constants.LAMBDA_ADVANCED_LOGGING_LEVELS.get(os.getenv(constants.LAMBDA_LOG_LEVEL_ENV))

def _get_powertools_log_level(self, level: Union[str, int, None]) -> Optional[str]:
"""Retrieve the log level for Powertools from the environment variable or level parameter.
If log level is an integer, we convert to its respective string level `logging.getLevelName()`.
If no log level is provided, we check env vars for the log level: POWERTOOLS_LOG_LEVEL_ENV and POWERTOOLS_LOG_LEVEL_LEGACY_ENV.
Parameters:
-----------
level : Union[str, int, None]
The specified log level as a string, integer, or None.
Environment variables
---------------------
POWERTOOLS_LOG_LEVEL : str
log level (e.g: INFO, DEBUG, WARNING, ERROR, CRITICAL)
LOG_LEVEL (Legacy) : str
log level (e.g: INFO, DEBUG, WARNING, ERROR, CRITICAL)
Returns:
--------
Optional[str]:
The corresponding logging level. Returns None if the log level is not explicitly specified.
""" # noqa E501

# Extract log level from Powertools Logger env vars
log_level_env = os.getenv(constants.POWERTOOLS_LOG_LEVEL_ENV) or os.getenv(
constants.POWERTOOLS_LOG_LEVEL_LEGACY_ENV,
)
# If level is an int (logging.INFO), return its respective string ("INFO")
if isinstance(level, int):
return level
return logging.getLevelName(level)

return level or log_level_env

def _determine_log_level(self, level: Union[str, int, None]) -> Union[str, int]:
"""Determine the effective log level considering Lambda and Powertools preferences.
It emits an UserWarning if Lambda ALC log level is lower than Logger log level.
Parameters:
-----------
level: Union[str, int, None]
The specified log level as a string, integer, or None.
Returns:
----------
Union[str, int]: The effective logging level.
"""

log_level: Optional[str] = level or os.getenv("LOG_LEVEL")
if log_level is None:
# This function consider the following order of precedence:
# 1 - If a log level is set using AWS Lambda Advanced Logging Controls, it sets it.
# 2 - If a log level is passed to the constructor, it sets it
# 3 - If a log level is set via setLevel, it sets it.
# 4 - If a log level is set via Powertools env variables, it sets it.
# 5 - If none of the above is true, the default log level applies INFO.

lambda_log_level = self._get_aws_lambda_log_level()
powertools_log_level = self._get_powertools_log_level(level)

if powertools_log_level and lambda_log_level:
# If Powertools log level is set and higher than AWS Lambda Advanced Logging Controls, emit a warning
if logging.getLevelName(lambda_log_level) > logging.getLevelName(powertools_log_level):
warnings.warn(
f"Current log level ({powertools_log_level}) does not match AWS Lambda Advanced Logging Controls "
f"minimum log level ({lambda_log_level}). This can lead to data loss, consider adjusting them.",
UserWarning,
stacklevel=2,
)

# AWS Lambda Advanced Logging Controls takes precedence over Powertools log level and we use this
if lambda_log_level:
return lambda_log_level

# Check if Powertools log level is None, which means it's not set
# We assume INFO as the default log level
if powertools_log_level is None:
return logging.INFO

return log_level.upper()
# Powertools log level is set, we use this
return powertools_log_level.upper()


def set_package_logger(
Expand Down
15 changes: 15 additions & 0 deletions aws_lambda_powertools/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@

POWERTOOLS_DEV_ENV: str = "POWERTOOLS_DEV"
POWERTOOLS_DEBUG_ENV: str = "POWERTOOLS_DEBUG"
POWERTOOLS_LOG_LEVEL_ENV: str = "POWERTOOLS_LOG_LEVEL"
POWERTOOLS_LOG_LEVEL_LEGACY_ENV: str = "LOG_LEVEL"
LAMBDA_LOG_LEVEL_ENV: str = "AWS_LAMBDA_LOG_LEVEL"

# Mapping of Lambda log levels to Python logging levels
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-logging.html#configuration-logging-log-levels
LAMBDA_ADVANCED_LOGGING_LEVELS = {
None: None,
"TRACE": "NOTSET",
"DEBUG": "DEBUG",
"INFO": "INFO",
"WARN": "WARNING",
"ERROR": "ERROR",
"FATAL": "CRITICAL",
}
2 changes: 1 addition & 1 deletion aws_lambda_powertools/shared/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Exposes version constant to avoid circular dependencies."""

VERSION = "2.26.1"
VERSION = "2.27.0"
4 changes: 2 additions & 2 deletions benchmark/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Globals:
POWERTOOLS_SERVICE_NAME: benchmark
POWERTOOLS_METRICS_NAMESPACE: LambdaPowertools
POWERTOOLS_LOGGER_LOG_EVENT: "true"
LOG_LEVEL: INFO
POWERTOOLS_LOG_LEVEL: INFO

Resources:
InstrumentedFunction:
Expand Down Expand Up @@ -45,4 +45,4 @@ Outputs:
InstrumentedLogGroup:
Value: !Ref InstrumentedLogGroup
ReferenceLogGroup:
Value: !Ref ReferenceLogGroup
Value: !Ref ReferenceLogGroup
2 changes: 1 addition & 1 deletion docs/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# v9.1.18
FROM squidfunk/mkdocs-material@sha256:f486dc932650bbbf71b83862715b842248973f1e240bf032c44ffe71906c40a4
FROM squidfunk/mkdocs-material@sha256:fc42bac0005e79c2b2dd576e72103b06932c30c7b7b455a1f9cd3d39b187d49d
# pip-compile --generate-hashes --output-file=requirements.txt requirements.in
COPY requirements.txt /tmp/
RUN pip install --require-hashes -r /tmp/requirements.txt
Loading

0 comments on commit e87b05a

Please sign in to comment.