Skip to content

Commit d4ca619

Browse files
committed
fix(docs): logger bringYourOwnFormatter snippet aws-powertools#1253
1 parent 86d31c3 commit d4ca619

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

Diff for: docs/snippets/logger/bringYourOwnFormatterClass.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1-
import { Logger } from '@aws-lambda-powertools/logger';
2-
import { MyCompanyLogFormatter } from './utils/formatters/MyCompanyLogFormatter';
1+
import { LogFormatter } from "@aws-lambda-powertools/logger";
2+
import {
3+
LogAttributes,
4+
UnformattedAttributes,
5+
} from "@aws-lambda-powertools/logger/lib/types";
36

4-
const logger = new Logger({
5-
logFormatter: new MyCompanyLogFormatter(),
6-
logLevel: 'DEBUG',
7-
serviceName: 'serverlessAirline',
8-
sampleRateValue: 0.5,
9-
persistentLogAttributes: {
10-
awsAccountId: process.env.AWS_ACCOUNT_ID,
11-
logger: {
12-
name: '@aws-lambda-powertools/logger',
13-
version: '0.0.1'
14-
}
15-
},
16-
});
7+
// Replace this line with your own type
8+
type MyCompanyLog = LogAttributes;
179

18-
export const handler = async (event, context): Promise<void> => {
10+
class MyCompanyLogFormatter extends LogFormatter {
11+
public formatAttributes(attributes: UnformattedAttributes): MyCompanyLog {
12+
return {
13+
message: attributes.message,
14+
service: attributes.serviceName,
15+
environment: attributes.environment,
16+
awsRegion: attributes.awsRegion,
17+
correlationIds: {
18+
awsRequestId: attributes.lambdaContext?.awsRequestId,
19+
xRayTraceId: attributes.xRayTraceId,
20+
},
21+
lambdaFunction: {
22+
name: attributes.lambdaContext?.functionName,
23+
arn: attributes.lambdaContext?.invokedFunctionArn,
24+
memoryLimitInMB: attributes.lambdaContext?.memoryLimitInMB,
25+
version: attributes.lambdaContext?.functionVersion,
26+
coldStart: attributes.lambdaContext?.coldStart,
27+
},
28+
logLevel: attributes.logLevel,
29+
timestamp: this.formatTimestamp(attributes.timestamp), // You can extend this function
30+
logger: {
31+
sampleRateValue: attributes.sampleRateValue,
32+
},
33+
};
34+
}
35+
}
1936

20-
logger.addContext(context);
21-
22-
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
23-
24-
};
37+
export { MyCompanyLogFormatter };

0 commit comments

Comments
 (0)