Skip to content

fix(logger): add setLevel function to set level programmatically #2320

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

Merged
Merged
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
19 changes: 17 additions & 2 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class Logger(logging.Logger): # lgtm [py/missing-call-to-init]
service : str, optional
service name to be appended in logs, by default "service_undefined"
level : str, int optional
logging.level, by default "INFO"
The level to set. Can be a string representing the level name: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
or an integer representing the level value: 10 for 'DEBUG', 20 for 'INFO', 30 for 'WARNING', 40 for 'ERROR', 50 for 'CRITICAL'. # noqa: E501
by default "INFO"
child: bool, optional
create a child Logger named <service>.<caller_file_name>, False by default
sample_rate: float, optional
Expand Down Expand Up @@ -327,7 +329,7 @@ def _configure_sampling(self):
try:
if self.sampling_rate and random.random() <= float(self.sampling_rate):
logger.debug("Setting log level to Debug due to sampling rate")
self.log_level = logging.DEBUG
self.setLevel(logging.DEBUG)
except ValueError:
raise InvalidLoggerSamplingRateError(
f"Expected a float value ranging 0 to 1, but received {self.sampling_rate} instead."
Expand Down Expand Up @@ -443,6 +445,19 @@ def decorate(event, context, *args, **kwargs):

return decorate

def setLevel(self, level: Union[str, int]):
"""
Set the logging level for the logger.

Parameters:
-----------
level str | int
The level to set. Can be a string representing the level name: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
or an integer representing the level value: 10 for 'DEBUG', 20 for 'INFO', 30 for 'WARNING', 40 for 'ERROR', 50 for 'CRITICAL'. # noqa: E501
"""
self.log_level = level
self._logger.setLevel(level)

def info(
self,
msg: object,
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,23 @@ def test_logger_level_env_var_as_int(monkeypatch, service_name):
Logger(service=service_name)


def test_logger_switch_between_levels(stdout, service_name):
# GIVEN a Loggers is initialized with INFO level
logger = Logger(service=service_name, level="INFO", stream=stdout)
logger.info("message info")

# WHEN we switch to DEBUG level
logger.setLevel(level="DEBUG")
logger.debug("message debug")

# THEN we must have different levels and messages in stdout
log_output = capture_multiple_logging_statements_output(stdout)
assert log_output[0]["level"] == "INFO"
assert log_output[0]["message"] == "message info"
assert log_output[1]["level"] == "DEBUG"
assert log_output[1]["message"] == "message debug"


def test_logger_record_caller_location(stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
Expand Down