Skip to content

Commit 288ce7e

Browse files
authored
fix(logger): ensure logs stream to stdout by default, not stderr (#2736)
1 parent 5ec1fa8 commit 288ce7e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ def __init__(
232232
)
233233
self.child = child
234234
self.logger_formatter = logger_formatter
235-
self.logger_handler = logger_handler or logging.StreamHandler(stream)
235+
self._stream = stream or sys.stdout
236+
self.logger_handler = logger_handler or logging.StreamHandler(self._stream)
236237
self.log_uncaught_exceptions = log_uncaught_exceptions
237238

238239
self._is_deduplication_disabled = resolve_truthy_env_var_choice(

tests/functional/test_logger.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,3 +945,17 @@ def test_logger_log_uncaught_exceptions(service_name, stdout):
945945
# THEN it should contain our custom exception hook with a copy of our logger
946946
assert isinstance(exception_hook, functools.partial)
947947
assert exception_hook.keywords.get("logger") == logger
948+
949+
950+
def test_stream_defaults_to_stdout(service_name, capsys):
951+
# GIVEN Logger is initialized without any explicit stream
952+
logger = Logger(service=service_name)
953+
msg = "testing stdout"
954+
955+
# WHEN logging statements are issued
956+
logger.info(msg)
957+
958+
# THEN we should default to standard output, not standard error.
959+
# NOTE: we can't assert on capsys.readouterr().err due to a known bug: https://github.com/pytest-dev/pytest/issues/5997
960+
log = json.loads(capsys.readouterr().out.strip())
961+
assert log["message"] == msg

0 commit comments

Comments
 (0)