-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_logger.py
1288 lines (956 loc) · 43.5 KB
/
test_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import inspect
import io
import json
import logging
import random
import re
import secrets
import string
import sys
from collections import namedtuple
from datetime import datetime, timezone
from typing import Any, Callable, Dict, Iterable, List, Optional, Union
import pytest
from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.logging.exceptions import InvalidLoggerSamplingRateError
from aws_lambda_powertools.logging.formatter import (
BasePowertoolsFormatter,
LambdaPowertoolsFormatter,
)
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.utilities.data_classes import S3Event, event_source
@pytest.fixture
def stdout():
return io.StringIO()
@pytest.fixture
def lambda_context():
lambda_context = {
"function_name": "test",
"memory_limit_in_mb": 128,
"invoked_function_arn": "arn:aws:lambda:eu-west-1:809313241:function:test",
"aws_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
}
return namedtuple("LambdaContext", lambda_context.keys())(*lambda_context.values())
@pytest.fixture
def lambda_event():
return {"greeting": "hello"}
@pytest.fixture
def service_name():
chars = string.ascii_letters + string.digits
return "".join(random.SystemRandom().choice(chars) for _ in range(15))
def capture_logging_output(stdout):
return json.loads(stdout.getvalue().strip())
def capture_multiple_logging_statements_output(stdout):
return [json.loads(line.strip()) for line in stdout.getvalue().split("\n") if line]
def test_setup_service_name(stdout, service_name):
# GIVEN Logger is initialized
# WHEN service is explicitly defined
logger = Logger(service=service_name, stream=stdout)
logger.info("Hello")
# THEN service field should be equals service given
log = capture_logging_output(stdout)
assert service_name == log["service"]
def test_setup_service_env_var(monkeypatch, stdout, service_name):
# GIVEN Logger is initialized
# WHEN service is explicitly defined via POWERTOOLS_SERVICE_NAME env
monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", service_name)
logger = Logger(stream=stdout)
logger.info("Hello")
# THEN service field should be equals POWERTOOLS_SERVICE_NAME value
log = capture_logging_output(stdout)
assert service_name == log["service"]
def test_setup_sampling_rate_env_var(monkeypatch, stdout, service_name):
# GIVEN Logger is initialized
# WHEN samping rate is explicitly set to 100% via POWERTOOLS_LOGGER_SAMPLE_RATE env
sampling_rate = "1"
monkeypatch.setenv("POWERTOOLS_LOGGER_SAMPLE_RATE", sampling_rate)
logger = Logger(service=service_name, stream=stdout)
logger.debug("I am being sampled")
# THEN sampling rate should be equals POWERTOOLS_LOGGER_SAMPLE_RATE value
# log level should be DEBUG
# and debug log statements should be in stdout
log = capture_logging_output(stdout)
assert sampling_rate == log["sampling_rate"]
assert "DEBUG" == log["level"]
assert "I am being sampled" == log["message"]
def test_inject_lambda_context(lambda_context, stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN a lambda function is decorated with logger
@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")
handler({}, lambda_context)
# THEN lambda contextual info should always be in the logs
log = capture_logging_output(stdout)
expected_logger_context_keys = (
"function_name",
"function_memory_size",
"function_arn",
"function_request_id",
)
for key in expected_logger_context_keys:
assert key in log
def test_inject_lambda_context_log_event_request(lambda_context, stdout, lambda_event, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN a lambda function is decorated with logger instructed to log event
@logger.inject_lambda_context(log_event=True)
def handler(event, context):
logger.info("Hello")
handler(lambda_event, lambda_context)
# THEN logger should log event received from Lambda
logged_event, _ = capture_multiple_logging_statements_output(stdout)
assert logged_event["message"] == lambda_event
def test_inject_lambda_context_log_event_request_env_var(
monkeypatch,
lambda_context,
stdout,
lambda_event,
service_name,
):
# GIVEN Logger is initialized
monkeypatch.setenv("POWERTOOLS_LOGGER_LOG_EVENT", "true")
logger = Logger(service=service_name, stream=stdout)
# WHEN a lambda function is decorated with logger instructed to log event
# via POWERTOOLS_LOGGER_LOG_EVENT env
@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")
handler(lambda_event, lambda_context)
# THEN logger should log event received from Lambda
logged_event, _ = capture_multiple_logging_statements_output(stdout)
assert logged_event["message"] == lambda_event
def test_inject_lambda_context_log_no_request_by_default(
monkeypatch,
lambda_context,
stdout,
lambda_event,
service_name,
):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN a lambda function is decorated with logger
@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")
handler(lambda_event, lambda_context)
# THEN logger should not log event received by lambda handler
log = capture_logging_output(stdout)
assert log["message"] != lambda_event
def test_inject_lambda_cold_start(lambda_context, stdout, service_name):
# cold_start can be false as it's a global variable in Logger module
# so we reset it to simulate the correct behaviour
# since Lambda will only import our logger lib once per concurrent execution
from aws_lambda_powertools.logging import logger
logger.is_cold_start = True
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN a lambda function is decorated with logger, and called twice
@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")
handler({}, lambda_context)
handler({}, lambda_context)
# THEN cold_start key should only be true in the first call
first_log, second_log = capture_multiple_logging_statements_output(stdout)
assert first_log["cold_start"] is True
assert second_log["cold_start"] is False
def test_logger_append_duplicated(stdout, service_name):
# GIVEN Logger is initialized with request_id field
logger = Logger(service=service_name, stream=stdout, request_id="value")
# WHEN `request_id` is appended to the existing structured log
# using a different value
logger.structure_logs(append=True, request_id="new_value")
logger.info("log")
# THEN subsequent log statements should have the latest value
log = capture_logging_output(stdout)
assert "new_value" == log["request_id"]
def test_logger_honors_given_exception_keys(stdout, service_name):
# GIVEN Logger is initialized with exception and exception_name fields
logger = Logger(service=service_name, stream=stdout)
# WHEN log level info
logger.info("log", exception="exception_value", exception_name="exception_name_value")
# THEN log statements should have these keys
log = capture_logging_output(stdout)
assert "exception_value" == log["exception"]
assert "exception_name_value" == log["exception_name"]
def test_logger_invalid_sampling_rate(service_name):
# GIVEN Logger is initialized
# WHEN sampling_rate non-numeric value
# THEN we should raise InvalidLoggerSamplingRateError
with pytest.raises(InvalidLoggerSamplingRateError):
Logger(service=service_name, stream=stdout, sampling_rate="TEST")
def test_inject_lambda_context_with_structured_log(lambda_context, stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN structure_logs has been used to add an additional key upfront
# and a lambda function is decorated with logger.inject_lambda_context
logger.structure_logs(append=True, additional_key="test")
@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")
handler({}, lambda_context)
# THEN lambda contextual info should always be in the logs
log = capture_logging_output(stdout)
expected_logger_context_keys = (
"function_name",
"function_memory_size",
"function_arn",
"function_request_id",
"additional_key",
)
for key in expected_logger_context_keys:
assert key in log
def test_logger_children_propagate_changes(stdout, service_name):
# GIVEN Loggers are initialized
# create child logger before parent to mimick
# importing logger from another module/file
# as loggers are created in global scope
child = Logger(stream=stdout, service=service_name, child=True)
parent = Logger(stream=stdout, service=service_name)
# WHEN a child Logger adds an additional key
child.structure_logs(append=True, customer_id="value")
# THEN child Logger changes should propagate to parent
# and subsequent log statements should have the latest value
parent.info("Hello parent")
child.info("Hello child")
parent_log, child_log = capture_multiple_logging_statements_output(stdout)
assert "customer_id" in parent_log
assert "customer_id" in child_log
assert child.parent.name == service_name
def test_logger_child_not_set_returns_same_logger(stdout):
# GIVEN two Loggers are initialized with the same service name
# WHEN child param isn't set
logger_one = Logger(service="something", stream=stdout)
logger_two = Logger(service="something", stream=stdout)
# THEN we should have two Logger instances
# however inner logger wise should be the same
assert id(logger_one) != id(logger_two)
assert logger_one._logger is logger_two._logger
assert logger_one.name is logger_two.name
# THEN we should also not see any duplicated logs
logger_one.info("One - Once")
logger_two.info("Two - Once")
logs = list(capture_multiple_logging_statements_output(stdout))
assert len(logs) == 2
def test_logger_level_case_insensitive(service_name):
# GIVEN a Loggers is initialized
# WHEN log level is set as "info" instead of "INFO"
logger = Logger(service=service_name, level="info")
# THEN we should correctly set log level as INFO
assert logger.level == logging.INFO
def test_logger_level_not_set(service_name):
# GIVEN a Loggers is initialized
# WHEN no log level was passed
logger = Logger(service=service_name)
# THEN we should default to INFO
assert logger.level == logging.INFO
def test_logger_level_as_int(service_name):
# GIVEN a Loggers is initialized
# WHEN log level is int
logger = Logger(service=service_name, level=logging.INFO)
# THEN we should be expected int (20, in this case)
assert logger.level == logging.INFO
def test_logger_level_env_var_as_int(monkeypatch, service_name):
# GIVEN Logger is initialized
# WHEN log level is explicitly defined via POWERTOOLS_LOG_LEVEL env as int
# THEN Logger should propagate ValueError
# since env vars can only be string
# and '50' is not a correct log level
monkeypatch.setenv(constants.POWERTOOLS_LOG_LEVEL_ENV, 50)
with pytest.raises(ValueError, match="Unknown level: '50'"):
Logger(service=service_name)
def test_logger_level_env_var_legacy_as_int(monkeypatch, service_name):
# GIVEN Logger is initialized
# WHEN log level is explicitly defined via legacy env LOG_LEVEL env as int
# THEN Logger should propagate ValueError
# since env vars can only be string
# and '50' is not a correct log level
monkeypatch.setenv(constants.POWERTOOLS_LOG_LEVEL_LEGACY_ENV, 50)
with pytest.raises(ValueError, match="Unknown level: '50'"):
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)
# WHEN log statement is run
logger.info("log")
# THEN 'location' field should have
# the correct caller resolution
caller_fn_name = inspect.currentframe().f_code.co_name
log = capture_logging_output(stdout)
assert caller_fn_name in log["location"]
def test_logger_do_not_log_twice_when_root_logger_is_setup(stdout, service_name):
# GIVEN Lambda configures the root logger with a handler
root_logger = logging.getLogger()
root_logger.addHandler(logging.StreamHandler(stream=stdout))
# WHEN we create a new Logger and child Logger
logger = Logger(service=service_name, stream=stdout)
child_logger = Logger(service=service_name, child=True, stream=stdout)
logger.info("PARENT")
child_logger.info("CHILD")
root_logger.info("ROOT")
# THEN it should only contain only two log entries
# since child's log records propagated to root logger should be rejected
logs = list(stdout.getvalue().strip().split("\n"))
assert len(logs) == 2
def test_logger_extra_kwargs(stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN `request_id` is an extra field in a log message to the existing structured log
fields = {"request_id": "blah"}
logger.info("with extra fields", extra=fields)
logger.info("without extra fields")
extra_fields_log, no_extra_fields_log = capture_multiple_logging_statements_output(stdout)
# THEN first log should have request_id field in the root structure
assert "request_id" in extra_fields_log
# THEN second log should not have request_id in the root structure
assert "request_id" not in no_extra_fields_log
def test_logger_arbitrary_fields_as_kwargs(stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN `request_id` is an arbitrary field in a log message to the existing structured log
fields = {"request_id": "blah"}
logger.info("with arbitrary fields", **fields)
logger.info("without extra fields")
extra_fields_log, no_extra_fields_log = capture_multiple_logging_statements_output(stdout)
# THEN first log should have request_id field in the root structure
assert "request_id" in extra_fields_log
# THEN second log should not have request_id in the root structure
assert "request_id" not in no_extra_fields_log
def test_logger_log_twice_when_log_filter_isnt_present_and_root_logger_is_setup(monkeypatch, stdout, service_name):
# GIVEN Lambda configures the root logger with a handler
root_logger = logging.getLogger()
root_logger.addHandler(logging.StreamHandler(stream=stdout))
# WHEN we create a new Logger and child Logger
# and log deduplication filter for child messages are disabled
# see #262 for more details on why this is needed for Pytest Live Log feature
monkeypatch.setenv(constants.LOGGER_LOG_DEDUPLICATION_ENV, "true")
logger = Logger(service=service_name, stream=stdout)
child_logger = Logger(service=service_name, child=True, stream=stdout)
logger.info("PARENT")
child_logger.info("CHILD")
# THEN it should only contain only two log entries
# since child's log records propagated to root logger should be rejected
logs = list(stdout.getvalue().strip().split("\n"))
assert len(logs) == 4
def test_logger_exception_extract_exception_name(stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN calling a logger.exception with a ValueError
try:
raise ValueError("something went wrong")
except Exception:
logger.exception("Received an exception")
# THEN we expect a "exception_name" to be "ValueError"
log = capture_logging_output(stdout)
assert "ValueError" == log["exception_name"]
def test_logger_set_correlation_id(lambda_context, stdout, service_name):
# GIVEN
logger = Logger(service=service_name, stream=stdout)
request_id = "xxx-111-222"
mock_event = {"requestContext": {"requestId": request_id}}
def handler(event, _):
logger.set_correlation_id(event["requestContext"]["requestId"])
logger.info("Foo")
# WHEN
handler(mock_event, lambda_context)
# THEN
log = capture_logging_output(stdout)
assert request_id == log["correlation_id"]
def test_logger_get_correlation_id(lambda_context, stdout, service_name):
# GIVEN a logger with a correlation_id set
logger = Logger(service=service_name, stream=stdout)
logger.set_correlation_id("foo")
# WHEN calling get_correlation_id
correlation_id = logger.get_correlation_id()
# THEN it should return the correlation_id
assert "foo" == correlation_id
def test_logger_set_correlation_id_path(lambda_context, stdout, service_name):
# GIVEN
logger = Logger(service=service_name, stream=stdout)
request_id = "xxx-111-222"
mock_event = {"requestContext": {"requestId": request_id}}
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
def handler(event, context):
logger.info("Foo")
# WHEN
handler(mock_event, lambda_context)
# THEN
log = capture_logging_output(stdout)
assert request_id == log["correlation_id"]
def test_logger_set_correlation_id_path_custom_functions(lambda_context, stdout, service_name):
# GIVEN an initialized Logger
# AND a Lambda handler decorated with a JMESPath expression using Powertools custom functions
logger = Logger(service=service_name, stream=stdout)
@logger.inject_lambda_context(correlation_id_path="Records[*].powertools_json(body).id")
def handler(event, context): ...
# WHEN handler is called
request_id = "xxx-111-222"
mock_event = {"Records": [{"body": json.dumps({"id": request_id})}]}
handler(mock_event, lambda_context)
# THEN there should be no exception and correlation ID should match
assert logger.get_correlation_id() == [request_id]
def test_logger_append_remove_keys(stdout, service_name):
# GIVEN a Logger is initialized
logger = Logger(service=service_name, stream=stdout)
extra_keys = {"request_id": "id", "context": "value"}
# WHEN keys are updated
logger.append_keys(**extra_keys)
logger.info("message with new keys")
# And removed
logger.remove_keys(extra_keys.keys())
logger.info("message after keys being removed")
# THEN additional keys should only be present in the first log statement
extra_keys_log, keys_removed_log = capture_multiple_logging_statements_output(stdout)
assert extra_keys.items() <= extra_keys_log.items()
assert (extra_keys.items() <= keys_removed_log.items()) is False
def test_logger_append_and_show_current_keys(stdout, service_name):
# GIVEN a Logger is initialized
logger = Logger(service=service_name, stream=stdout)
extra_keys = {"request_id": "id", "context": "value"}
# WHEN keys are updated
logger.append_keys(**extra_keys)
# THEN appended keys must be present in logger
current_keys = logger.get_current_keys()
assert "request_id" in current_keys
assert "context" in current_keys
def test_logger_formatter_without_get_current_keys_method(stdout, service_name):
class CustomFormatter(BasePowertoolsFormatter):
def append_keys(self, **additional_keys):
# Fake method
pass
def clear_state(self) -> None:
# Fake method
pass
custom_formater = CustomFormatter()
# GIVEN a Logger is initialized with a Logger Formatter from scratch
logger = Logger(service=service_name, stream=stdout, logger_formatter=custom_formater)
extra_keys = {"request_id": "id", "context": "value"}
# WHEN keys are updated
logger.append_keys(**extra_keys)
# THEN the appended keys will not persist
# unless the customer implements the required methods and persists the log_format
assert logger.get_current_keys() == {}
def test_logger_custom_formatter(stdout, service_name, lambda_context):
class CustomFormatter(BasePowertoolsFormatter):
custom_format = {}
def append_keys(self, **additional_keys):
self.custom_format.update(additional_keys)
def remove_keys(self, keys: Iterable[str]):
for key in keys:
self.custom_format.pop(key, None)
def clear_state(self):
self.custom_format.clear()
def format(self, record: logging.LogRecord) -> str: # noqa: A003
return json.dumps(
{
"message": super().format(record),
"timestamp": self.formatTime(record),
"my_default_key": "test",
**self.custom_format,
},
)
custom_formatter = CustomFormatter()
# GIVEN a Logger is initialized with a custom formatter
logger = Logger(service=service_name, stream=stdout, logger_formatter=custom_formatter)
# WHEN a lambda function is decorated with logger
@logger.inject_lambda_context(correlation_id_path="foo")
def handler(event, context):
logger.info("Hello")
handler({"foo": "value"}, lambda_context)
lambda_context_keys = (
"function_name",
"function_memory_size",
"function_arn",
"function_request_id",
)
log = capture_logging_output(stdout)
# THEN custom key should always be present
# and lambda contextual info should also be in the logs
# and get_correlation_id should return None
assert "my_default_key" in log
assert all(k in log for k in lambda_context_keys)
assert log["correlation_id"] == "value"
assert logger.get_correlation_id() is None
def test_logger_custom_powertools_formatter_clear_state(stdout, service_name, lambda_context):
class CustomFormatter(LambdaPowertoolsFormatter):
def __init__(
self,
json_serializer: Optional[Callable[[Dict], str]] = None,
json_deserializer: Optional[Callable[[Union[Dict, str, bool, int, float]], str]] = None,
json_default: Optional[Callable[[Any], Any]] = None,
datefmt: Optional[str] = None,
use_datetime_directive: bool = False,
log_record_order: Optional[List[str]] = None,
utc: bool = False,
**kwargs,
):
super().__init__(
json_serializer,
json_deserializer,
json_default,
datefmt,
use_datetime_directive,
log_record_order,
utc,
**kwargs,
)
custom_formatter = CustomFormatter()
# GIVEN a Logger is initialized with a custom formatter
logger = Logger(service=service_name, stream=stdout, logger_formatter=custom_formatter)
# WHEN a lambda function is decorated with logger
# and state is to be cleared in the next invocation
@logger.inject_lambda_context(clear_state=True)
def handler(event, context):
if event.get("add_key"):
logger.append_keys(my_key="value")
logger.info("Hello")
handler({"add_key": True}, lambda_context)
handler({}, lambda_context)
lambda_context_keys = (
"function_name",
"function_memory_size",
"function_arn",
"function_request_id",
)
first_log, second_log = capture_multiple_logging_statements_output(stdout)
# THEN my_key should only present once
# and lambda contextual info should also be in both logs
assert "my_key" in first_log
assert "my_key" not in second_log
assert all(k in first_log for k in lambda_context_keys)
assert all(k in second_log for k in lambda_context_keys)
def test_logger_custom_formatter_has_standard_and_custom_keys(stdout, service_name, lambda_context):
class CustomFormatter(LambdaPowertoolsFormatter): ...
# GIVEN a Logger is initialized with a custom formatter
logger = Logger(service=service_name, stream=stdout, logger_formatter=CustomFormatter(), my_key="value")
# WHEN a lambda function is decorated with logger
@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")
handler({}, lambda_context)
standard_keys = (
"level",
"location",
"message",
"timestamp",
"service",
"cold_start",
"function_name",
"function_memory_size",
"function_arn",
"function_request_id",
)
log = capture_logging_output(stdout)
# THEN all standard keys should be available
assert all(k in log for k in standard_keys)
assert "my_key" in log
def test_logger_custom_handler(lambda_context, service_name, tmp_path):
# GIVEN a Logger is initialized with a FileHandler
log_file = tmp_path / "log.json"
handler = logging.FileHandler(filename=log_file)
logger = Logger(service=service_name, logger_handler=handler)
# WHEN a log statement happens
@logger.inject_lambda_context
def handler(event, context):
logger.info("custom handler")
handler({}, lambda_context)
# THEN we should output to a file not stdout
log = log_file.read_text()
assert "custom handler" in log
def test_clear_state_on_inject_lambda_context(lambda_context, stdout, service_name):
# GIVEN
logger = Logger(service=service_name, stream=stdout)
# WHEN clear_state is set and a key was conditionally added in the first invocation
@logger.inject_lambda_context(clear_state=True)
def handler(event, context):
if event.get("add_key"):
logger.append_keys(my_key="value")
logger.info("Foo")
# THEN custom key should only exist in the first log
handler({"add_key": True}, lambda_context)
handler({}, lambda_context)
first_log, second_log = capture_multiple_logging_statements_output(stdout)
assert "my_key" in first_log
assert "my_key" not in second_log
def test_clear_state_keeps_standard_keys(lambda_context, stdout, service_name):
# GIVEN
logger = Logger(service=service_name, stream=stdout)
standard_keys = ["level", "location", "message", "timestamp", "service"]
# WHEN clear_state is set
@logger.inject_lambda_context(clear_state=True)
def handler(event, context):
logger.info("Foo")
# THEN all standard keys should be available as usual
handler({}, lambda_context)
handler({}, lambda_context)
first_log, second_log = capture_multiple_logging_statements_output(stdout)
for key in standard_keys:
assert key in first_log
assert key in second_log
def test_clear_state_keeps_custom_keys(lambda_context, stdout, service_name):
# GIVEN
location_format = "%(module)s.%(funcName)s:clear_state"
logger = Logger(service=service_name, stream=stdout, location=location_format, custom_key="foo")
# WHEN clear_state is set
@logger.inject_lambda_context(clear_state=True)
def handler(event, context):
logger.info("Foo")
# THEN all standard keys should be available as usual
handler({}, lambda_context)
handler({}, lambda_context)
first_log, second_log = capture_multiple_logging_statements_output(stdout)
for log in (first_log, second_log):
assert "foo" == log["custom_key"]
assert "test_logger.handler:clear_state" == log["location"]
def test_clear_state_keeps_exception_keys(lambda_context, stdout, service_name):
# GIVEN
logger = Logger(service=service_name, stream=stdout)
# WHEN clear_state is set and an exception was logged
@logger.inject_lambda_context(clear_state=True)
def handler(event, context):
try:
raise ValueError("something went wrong")
except Exception:
logger.exception("Received an exception")
# THEN we expect a "exception_name" to be "ValueError"
handler({}, lambda_context)
log = capture_logging_output(stdout)
assert "ValueError" == log["exception_name"]
def test_inject_lambda_context_allows_handler_with_kwargs(lambda_context, stdout, service_name):
# GIVEN
logger = Logger(service=service_name, stream=stdout)
# WHEN
@logger.inject_lambda_context(clear_state=True)
def handler(event, context, my_custom_option=None):
pass
# THEN
handler({}, lambda_context, my_custom_option="blah")
@pytest.mark.parametrize("utc", [False, True])
def test_use_datetime(stdout, service_name, utc):
# GIVEN
logger = Logger(
service=service_name,
stream=stdout,
datefmt="custom timestamp: milliseconds=%F microseconds=%f timezone=%z",
use_datetime_directive=True,
utc=utc,
)
# WHEN a log statement happens
logger.info({})
# THEN the timestamp has the appropriate formatting
log = capture_logging_output(stdout)
expected_tz = datetime.now().astimezone(timezone.utc if utc else None).strftime("%z")
assert re.fullmatch(
f"custom timestamp: milliseconds=[0-9]+ microseconds=[0-9]+ timezone={re.escape(expected_tz)}",
log["timestamp"],
)
@pytest.mark.parametrize("utc", [False, True])
def test_use_rfc3339_iso8601(stdout, service_name, utc):
# GIVEN
logger = Logger(service=service_name, stream=stdout, use_rfc3339=True, utc=utc)
RFC3339_REGEX = r"^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$"
# WHEN a log statement happens
logger.info({})
# THEN the timestamp has the appropriate formatting
log = capture_logging_output(stdout)
assert re.fullmatch(RFC3339_REGEX, log["timestamp"]) # "2022-10-27T17:42:26.841+0200"
def test_inject_lambda_context_log_event_request_data_classes(lambda_context, stdout, lambda_event, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# WHEN a lambda function is decorated with logger instructed to log event
# AND the event is an event source data class
@event_source(data_class=S3Event)
@logger.inject_lambda_context(log_event=True)
def handler(event, context):
logger.info("Hello")
handler(lambda_event, lambda_context)
# THEN logger should log event received from Lambda
logged_event, _ = capture_multiple_logging_statements_output(stdout)
assert logged_event["message"] == lambda_event
def test_inject_lambda_context_with_additional_args(lambda_context, stdout, service_name):
# GIVEN Logger is initialized
logger = Logger(service=service_name, stream=stdout)
# AND a handler that use additional parameters
@logger.inject_lambda_context
def handler(event, context, planet, str_end="."):
logger.info(f"Hello {planet}{str_end}")
handler({}, lambda_context, "World", str_end="!")
# THEN the decorator should included them
log = capture_logging_output(stdout)
assert log["message"] == "Hello World!"
def test_logger_log_uncaught_exceptions(service_name, stdout):
# GIVEN an initialized Logger is set with log_uncaught_exceptions
logger = Logger(service=service_name, stream=stdout, log_uncaught_exceptions=True)
# WHEN Python's exception hook is inspected
exception_hook = sys.excepthook
# THEN it should contain our custom exception hook with a copy of our logger
assert isinstance(exception_hook, functools.partial)
assert exception_hook.keywords.get("logger") == logger
def test_stream_defaults_to_stdout(service_name, capsys):
# GIVEN Logger is initialized without any explicit stream
logger = Logger(service=service_name)
msg = "testing stdout"
# WHEN logging statements are issued
logger.info(msg)
# THEN we should default to standard output, not standard error.
# NOTE: we can't assert on capsys.readouterr().err due to a known bug: https://github.com/pytest-dev/pytest/issues/5997
log = json.loads(capsys.readouterr().out.strip())
assert log["message"] == msg
def test_logger_logs_stack_trace_with_default_value(service_name, stdout):
# GIVEN a Logger instance with serialize_stacktrace default value = True
logger = Logger(service=service_name, stream=stdout)
# WHEN invoking a Lambda
def handler(event, context):
try:
raise ValueError("something went wrong")
except Exception:
logger.exception("Received an exception")
# THEN we expect a "stack_trace" in log
handler({}, lambda_context)
log = capture_logging_output(stdout)
assert "stack_trace" in log
def test_logger_logs_stack_trace_with_non_default_value(service_name, stdout):
# GIVEN a Logger instance with serialize_stacktrace = False
logger = Logger(service=service_name, stream=stdout, serialize_stacktrace=False)
# WHEN invoking a Lambda
def handler(event, context):
try:
raise ValueError("something went wrong")
except Exception:
logger.exception("Received an exception")
# THEN we expect a "stack_trace" not in log
handler({}, lambda_context)
log = capture_logging_output(stdout)
assert "stack_trace" not in log