File tree 3 files changed +28
-0
lines changed
aws_lambda_powertools/logging
3 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
7
7
## [ Unreleased]
8
8
9
+ ## [ 1.1.3] - 2020-08-18
10
+ ### Fixed
11
+ - ** Logger** : Logs emitted twice, structured and unstructured, due to Lambda configuring the root handler
12
+
9
13
## [ 1.1.2] - 2020-08-16
10
14
### Fixed
11
15
- ** Docs** : Clarify confusion on Tracer reuse and ` auto_patch=False ` statement
Original file line number Diff line number Diff line change @@ -168,6 +168,13 @@ def _get_caller_filename(self):
168
168
def _init_logger (self , ** kwargs ):
169
169
"""Configures new logger"""
170
170
171
+ # Lambda by default configures the root logger handler
172
+ # therefore, we need to remove it to prevent messages being logged twice
173
+ # when customers use our Logger
174
+ logger .debug ("Removing Lambda root handler whether it exists" )
175
+ root_logger = logging .getLogger ()
176
+ root_logger .handlers .clear ()
177
+
171
178
# Skip configuration if it's a child logger to prevent
172
179
# multiple handlers being attached as well as different sampling mechanisms
173
180
# and multiple messages from being logged as handlers can be duplicated
Original file line number Diff line number Diff line change @@ -359,3 +359,20 @@ def test_logger_record_caller_location(stdout):
359
359
caller_fn_name = inspect .currentframe ().f_code .co_name
360
360
log = capture_logging_output (stdout )
361
361
assert caller_fn_name in log ["location" ]
362
+
363
+
364
+ def test_logger_do_not_log_twice (stdout ):
365
+ # GIVEN Lambda configures the root logger with a handler
366
+ logging .basicConfig (format = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s" , level = "INFO" )
367
+ root_logger = logging .getLogger ()
368
+ root_logger .addHandler (logging .StreamHandler (stream = stdout ))
369
+
370
+ # WHEN we create a new Logger
371
+ logger = Logger (stream = stdout )
372
+ logger .info ("hello" )
373
+
374
+ # THEN it should fail to unpack because root logger handler
375
+ # should be removed as part of our Logger initialization
376
+ assert not root_logger .handlers
377
+ with pytest .raises (ValueError , match = r".*expected 2, got 1.*" ):
378
+ [log_one , log_two ] = stdout .getvalue ().strip ().split ("\n " )
You can’t perform that action at this time.
0 commit comments