Skip to content

Commit e3f935c

Browse files
authored
improv(logger): switch key order for better readability (#3560)
1 parent f0bdf3c commit e3f935c

File tree

7 files changed

+85
-90
lines changed

7 files changed

+85
-90
lines changed

docs/core/logger.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ In each case, the printed log will look like this:
148148

149149
```json hl_lines="2-6"
150150
{
151+
"level": "INFO",
152+
"message": "This is an INFO log with some context",
153+
"timestamp": "2021-12-12T21:21:08.921Z",
154+
"service": "serverlessAirline",
151155
"cold_start": true,
152156
"function_arn": "arn:aws:lambda:eu-west-1:123456789012:function:shopping-cart-api-lambda-prod-eu-west-1",
153157
"function_memory_size": 128,
154158
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e812345678",
155159
"function_name": "shopping-cart-api-lambda-prod-eu-west-1",
156-
"level": "INFO",
157-
"message": "This is an INFO log with some context",
158-
"service": "serverlessAirline",
159-
"timestamp": "2021-12-12T21:21:08.921Z",
160160
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
161161
}
162162
```

examples/snippets/logger/customTimezoneOutput.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
{
33
"level": "INFO",
44
"message": "Hello, World!",
5-
"sampling_rate": 0,
6-
"service": "serverlessAirline",
75
"timestamp": "2024-07-01T11:00:37.886Z",
6+
"service": "serverlessAirline",
7+
"sampling_rate": 0,
88
"xray_trace_id": "1-66828c55-2bb635c65eb609c820ebe7bc"
99
},
1010
{
1111
"level": "INFO",
1212
"message": "Ciao, Mondo!",
13-
"sampling_rate": 0,
14-
"service": "serverlessAirline",
1513
"timestamp": "2024-07-01T13:00:37.934+02:00",
14+
"service": "serverlessAirline",
15+
"sampling_rate": 0,
1616
"xray_trace_id": "1-66828c55-2bb635c65eb609c820ebe7bc"
1717
}
1818
]
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"timestamp": "2024-09-03T02:59:06.603Z",
3-
"additionalKey": "additionalValue",
42
"level": "INFO",
53
"message": "Hello, World!",
6-
"sampling_rate": 0,
4+
"timestamp": "2024-09-03T02:59:06.603Z",
75
"service": "serverlessAirline",
6+
"additionalKey": "additionalValue",
7+
"sampling_rate": 0,
88
"xray_trace_id": "1-66d67b7a-79bc7b2346b32af01b437cf8"
9-
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"level": "INFO",
33
"message": "Serialize with custom serializer",
4-
"sampling_rate": 0,
5-
"service": "serverlessAirline",
64
"timestamp": "2024-07-07T09:52:14.212Z",
5+
"service": "serverlessAirline",
6+
"sampling_rate": 0,
77
"xray_trace_id": "1-668a654d-396c646b760ee7d067f32f18",
8-
"serializedValue": [1, 2, 3]
8+
"serializedValue": [
9+
1,
10+
2,
11+
3
12+
]
913
}

packages/logger/src/formatter/PowertoolsLogFormatter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ class PowertoolsLogFormatter extends LogFormatter {
4444
const baseAttributes: Partial<PowertoolsStandardKeys> &
4545
Partial<PowertoolsLambdaContextKeys> &
4646
LogAttributes = {
47+
level: attributes.logLevel,
48+
message: attributes.message,
49+
timestamp: this.formatTimestamp(attributes.timestamp),
50+
service: attributes.serviceName,
4751
cold_start: attributes.lambdaContext?.coldStart,
4852
function_arn: attributes.lambdaContext?.invokedFunctionArn,
4953
function_memory_size: attributes.lambdaContext?.memoryLimitInMB,
5054
function_name: attributes.lambdaContext?.functionName,
5155
function_request_id: attributes.lambdaContext?.awsRequestId,
52-
level: attributes.logLevel,
53-
message: attributes.message,
5456
sampling_rate: attributes.sampleRateValue,
55-
service: attributes.serviceName,
56-
timestamp: this.formatTimestamp(attributes.timestamp),
5757
xray_trace_id: attributes.xRayTraceId,
5858
};
5959

packages/logger/tests/unit/formatters.test.ts

+60-69
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe('Formatters', () => {
185185
it('when `logRecordOrder` is set, it orders the attributes in the log item', () => {
186186
// Prepare
187187
const formatter = new PowertoolsLogFormatter({
188-
logRecordOrder: ['message', 'timestamp', 'serviceName', 'environment'],
188+
logRecordOrder: ['message', 'timestamp', 'service'],
189189
});
190190
const additionalLogAttributes: LogAttributes = {};
191191

@@ -198,21 +198,19 @@ describe('Formatters', () => {
198198
const response = value.getAttributes();
199199

200200
// Assess
201-
expect(JSON.stringify(response)).toEqual(
202-
JSON.stringify({
203-
message: 'This is a WARN log',
204-
timestamp: '2016-06-20T12:08:10.000Z',
205-
cold_start: true,
206-
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
207-
function_memory_size: '123',
208-
function_name: 'my-lambda-function',
209-
function_request_id: 'abcdefg123456789',
210-
level: 'WARN',
211-
sampling_rate: 0.25,
212-
service: 'hello-world',
213-
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
214-
})
215-
);
201+
expect(response).toStrictEqual({
202+
message: 'This is a WARN log',
203+
timestamp: '2016-06-20T12:08:10.000Z',
204+
service: 'hello-world',
205+
level: 'WARN',
206+
cold_start: true,
207+
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
208+
function_memory_size: '123',
209+
function_name: 'my-lambda-function',
210+
function_request_id: 'abcdefg123456789',
211+
sampling_rate: 0.25,
212+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
213+
});
216214
});
217215

218216
it('when `logRecordOrder` is set, it orders the attributes in the log item taking `additionalLogAttributes` into consideration', () => {
@@ -222,8 +220,8 @@ describe('Formatters', () => {
222220
'message',
223221
'additional_key',
224222
'timestamp',
225-
'serviceName',
226-
'environment',
223+
'level',
224+
'service',
227225
]),
228226
});
229227
const additionalLogAttributes: LogAttributes = {
@@ -240,23 +238,21 @@ describe('Formatters', () => {
240238
const response = value.getAttributes();
241239

242240
// Assess
243-
expect(JSON.stringify(response)).toEqual(
244-
JSON.stringify({
245-
message: 'This is a WARN log',
246-
additional_key: 'additional_value',
247-
timestamp: '2016-06-20T12:08:10.000Z',
248-
cold_start: true,
249-
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
250-
function_memory_size: '123',
251-
function_name: 'my-lambda-function',
252-
function_request_id: 'abcdefg123456789',
253-
level: 'WARN',
254-
sampling_rate: 0.25,
255-
service: 'hello-world',
256-
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
257-
another_key: 'another_value',
258-
})
259-
);
241+
expect(response).toStrictEqual({
242+
message: 'This is a WARN log',
243+
additional_key: 'additional_value',
244+
timestamp: '2016-06-20T12:08:10.000Z',
245+
level: 'WARN',
246+
service: 'hello-world',
247+
cold_start: true,
248+
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
249+
function_memory_size: '123',
250+
function_name: 'my-lambda-function',
251+
function_request_id: 'abcdefg123456789',
252+
sampling_rate: 0.25,
253+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
254+
another_key: 'another_value',
255+
});
260256
});
261257

262258
it('when `logRecordOrder` is set, even if a key does not exist in attributes, it orders the attributes correctly', () => {
@@ -267,8 +263,7 @@ describe('Formatters', () => {
267263
'additional_key',
268264
'not_present',
269265
'timestamp',
270-
'serviceName',
271-
'environment',
266+
'level',
272267
],
273268
});
274269
const additionalLogAttributes: LogAttributes = {
@@ -284,22 +279,20 @@ describe('Formatters', () => {
284279
const response = value.getAttributes();
285280

286281
// Assess
287-
expect(JSON.stringify(response)).toEqual(
288-
JSON.stringify({
289-
message: 'This is a WARN log',
290-
additional_key: 'additional_value',
291-
timestamp: '2016-06-20T12:08:10.000Z',
292-
cold_start: true,
293-
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
294-
function_memory_size: '123',
295-
function_name: 'my-lambda-function',
296-
function_request_id: 'abcdefg123456789',
297-
level: 'WARN',
298-
sampling_rate: 0.25,
299-
service: 'hello-world',
300-
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
301-
})
302-
);
282+
expect(response).toStrictEqual({
283+
message: 'This is a WARN log',
284+
additional_key: 'additional_value',
285+
timestamp: '2016-06-20T12:08:10.000Z',
286+
level: 'WARN',
287+
service: 'hello-world',
288+
cold_start: true,
289+
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
290+
function_memory_size: '123',
291+
function_name: 'my-lambda-function',
292+
function_request_id: 'abcdefg123456789',
293+
sampling_rate: 0.25,
294+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
295+
});
303296
});
304297

305298
it('when logRecordOrder is not set, it will not order the attributes in the log item', () => {
@@ -318,22 +311,20 @@ describe('Formatters', () => {
318311
const response = value.getAttributes();
319312

320313
// Assess
321-
expect(JSON.stringify(response)).toEqual(
322-
JSON.stringify({
323-
cold_start: true,
324-
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
325-
function_memory_size: '123',
326-
function_name: 'my-lambda-function',
327-
function_request_id: 'abcdefg123456789',
328-
level: 'WARN',
329-
message: 'This is a WARN log',
330-
sampling_rate: 0.25,
331-
service: 'hello-world',
332-
timestamp: '2016-06-20T12:08:10.000Z',
333-
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
334-
additional_key: 'additional_value',
335-
})
336-
);
314+
expect(response).toStrictEqual({
315+
level: 'WARN',
316+
message: 'This is a WARN log',
317+
timestamp: '2016-06-20T12:08:10.000Z',
318+
service: 'hello-world',
319+
cold_start: true,
320+
function_arn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
321+
function_memory_size: '123',
322+
function_name: 'my-lambda-function',
323+
function_request_id: 'abcdefg123456789',
324+
sampling_rate: 0.25,
325+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
326+
additional_key: 'additional_value',
327+
});
337328
});
338329

339330
// #region format errors

packages/logger/tests/unit/initializeLogger.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ describe('Log levels', () => {
151151
{
152152
level: 'INFO',
153153
message: 'Hello, world!',
154-
sampling_rate: 0,
155-
service: 'hello-world',
156154
timestamp: '2016-06-20T12:08:10.000Z',
155+
service: 'hello-world',
156+
sampling_rate: 0,
157157
xray_trace_id: '1-abcdef12-3456abcdef123456abcdef12',
158158
},
159159
null,

0 commit comments

Comments
 (0)