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" ;
3
6
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 ;
17
9
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
+ }
19
36
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