Skip to content

Commit e1539ac

Browse files
committed
test: logRecordOrder option during formatting
1 parent c743eb0 commit e1539ac

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,60 @@ describe('Class: PowertoolsLogFormatter', () => {
102102
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
103103
});
104104
});
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+
});
105159
});
106160

107161
describe('Method: formatError', () => {

0 commit comments

Comments
 (0)