@@ -982,6 +982,93 @@ describe('Class: Logger', () => {
982
982
983
983
} ) ;
984
984
985
+ test ( 'when used as decorator with the log event flag enabled, it logs the event' , async ( ) => {
986
+
987
+ // Prepare
988
+ const logger = new Logger ( {
989
+ logLevel : 'DEBUG' ,
990
+ } ) ;
991
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , 'info' ) . mockImplementation ( ) ;
992
+
993
+ type CustomEvent = { user_id : string } ;
994
+
995
+ class LambdaFunction implements LambdaInterface {
996
+
997
+ @logger . injectLambdaContext ( { logEvent : true } )
998
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
999
+ // @ts -ignore
1000
+ public handler < TResult > ( _event : CustomEvent , _context : Context , _callback : Callback < TResult > ) : void | Promise < TResult > {
1001
+ return ;
1002
+ }
1003
+ }
1004
+
1005
+ // Act
1006
+ await new LambdaFunction ( ) . handler ( { user_id : '123456' } , dummyContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
1007
+
1008
+ // Assess
1009
+ expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
1010
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
1011
+ cold_start : true ,
1012
+ function_arn : 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
1013
+ function_memory_size : 128 ,
1014
+ function_name : 'foo-bar-function' ,
1015
+ function_request_id : 'c6af9ac6-7b61-11e6-9a41-93e812345678' ,
1016
+ level : 'INFO' ,
1017
+ message : 'Lambda invocation event' ,
1018
+ service : 'hello-world' ,
1019
+ timestamp : '2016-06-20T12:08:10.000Z' ,
1020
+ xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
1021
+ event : {
1022
+ user_id : '123456'
1023
+ }
1024
+ } ) ) ;
1025
+
1026
+ } ) ;
1027
+
1028
+ test ( 'when used as decorator without options, but POWERTOOLS_LOGGER_LOG_EVENT env var is set to true, it logs the event' , async ( ) => {
1029
+
1030
+ // Prepare
1031
+ process . env . POWERTOOLS_LOGGER_LOG_EVENT = 'true' ;
1032
+ const logger = new Logger ( {
1033
+ logLevel : 'DEBUG' ,
1034
+ } ) ;
1035
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , 'info' ) . mockImplementation ( ) ;
1036
+
1037
+ type CustomEvent = { user_id : string } ;
1038
+
1039
+ class LambdaFunction implements LambdaInterface {
1040
+
1041
+ @logger . injectLambdaContext ( )
1042
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1043
+ // @ts -ignore
1044
+ public handler < TResult > ( _event : CustomEvent , _context : Context , _callback : Callback < TResult > ) : void | Promise < TResult > {
1045
+ return ;
1046
+ }
1047
+ }
1048
+
1049
+ // Act
1050
+ await new LambdaFunction ( ) . handler ( { user_id : '123456' } , dummyContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
1051
+
1052
+ // Assess
1053
+ expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
1054
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith ( 1 , JSON . stringify ( {
1055
+ cold_start : true ,
1056
+ function_arn : 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
1057
+ function_memory_size : 128 ,
1058
+ function_name : 'foo-bar-function' ,
1059
+ function_request_id : 'c6af9ac6-7b61-11e6-9a41-93e812345678' ,
1060
+ level : 'INFO' ,
1061
+ message : 'Lambda invocation event' ,
1062
+ service : 'hello-world' ,
1063
+ timestamp : '2016-06-20T12:08:10.000Z' ,
1064
+ xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
1065
+ event : {
1066
+ user_id : '123456'
1067
+ }
1068
+ } ) ) ;
1069
+
1070
+ } ) ;
1071
+
985
1072
} ) ;
986
1073
987
1074
describe ( 'Method: refreshSampleRateCalculation' , ( ) => {
0 commit comments