@@ -102,6 +102,60 @@ describe('Class: PowertoolsLogFormatter', () => {
102
102
xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
103
103
} ) ;
104
104
} ) ;
105
+
106
+ test ( 'when logRecordOrder is set, it orders the attributes in the log item' , ( ) => {
107
+ // Prepare
108
+ const formatter = new PowertoolsLogFormatter ( {
109
+ logRecordOrder : [ 'message' , 'timestamp' , 'serviceName' , 'environment' ] ,
110
+ } ) ;
111
+ const unformattedAttributes : UnformattedAttributes = {
112
+ sampleRateValue : 0.25 ,
113
+ awsRegion : 'eu-west-1' ,
114
+ environment : 'prod' ,
115
+ serviceName : 'hello-world' ,
116
+ xRayTraceId : '1-5759e988-bd862e3fe1be46a994272793' ,
117
+ logLevel : 'WARN' ,
118
+ timestamp : new Date ( ) ,
119
+ message : 'This is a WARN log' ,
120
+ error : new Error ( 'Something happened!' ) ,
121
+ lambdaContext : {
122
+ functionName : 'my-lambda-function' ,
123
+ memoryLimitInMB : '123' ,
124
+ functionVersion : '1.23.3' ,
125
+ coldStart : true ,
126
+ invokedFunctionArn :
127
+ 'arn:aws:lambda:eu-west-1:123456789012:function:Example' ,
128
+ awsRequestId : 'abcdefg123456789' ,
129
+ } ,
130
+ } ;
131
+ const additionalLogAttributes : LogAttributes = { } ;
132
+
133
+ // Act
134
+ const value = formatter . formatAttributes (
135
+ unformattedAttributes ,
136
+ additionalLogAttributes
137
+ ) ;
138
+
139
+ const response = value . getAttributes ( ) ;
140
+
141
+ // Assess
142
+ expect ( JSON . stringify ( response ) ) . toEqual (
143
+ JSON . stringify ( {
144
+ message : 'This is a WARN log' ,
145
+ timestamp : '2016-06-20T12:08:10.000Z' ,
146
+ cold_start : true ,
147
+ function_arn :
148
+ 'arn:aws:lambda:eu-west-1:123456789012:function:Example' ,
149
+ function_memory_size : '123' ,
150
+ function_name : 'my-lambda-function' ,
151
+ function_request_id : 'abcdefg123456789' ,
152
+ level : 'WARN' ,
153
+ sampling_rate : 0.25 ,
154
+ service : 'hello-world' ,
155
+ xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
156
+ } )
157
+ ) ;
158
+ } ) ;
105
159
} ) ;
106
160
107
161
describe ( 'Method: formatError' , ( ) => {
0 commit comments