-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
86d31c3
commit fdbba32
Showing
1 changed file
with
34 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { MyCompanyLogFormatter } from './utils/formatters/MyCompanyLogFormatter'; | ||
import { LogFormatter } from "@aws-lambda-powertools/logger"; | ||
import { | ||
LogAttributes, | ||
UnformattedAttributes, | ||
} from "@aws-lambda-powertools/logger/lib/types"; | ||
|
||
const logger = new Logger({ | ||
logFormatter: new MyCompanyLogFormatter(), | ||
logLevel: 'DEBUG', | ||
serviceName: 'serverlessAirline', | ||
sampleRateValue: 0.5, | ||
persistentLogAttributes: { | ||
awsAccountId: process.env.AWS_ACCOUNT_ID, | ||
logger: { | ||
name: '@aws-lambda-powertools/logger', | ||
version: '0.0.1' | ||
} | ||
}, | ||
}); | ||
// Replace this line with your own type | ||
type MyCompanyLog = LogAttributes; | ||
|
||
export const handler = async (event, context): Promise<void> => { | ||
class MyCompanyLogFormatter extends LogFormatter { | ||
public formatAttributes(attributes: UnformattedAttributes): MyCompanyLog { | ||
return { | ||
message: attributes.message, | ||
service: attributes.serviceName, | ||
environment: attributes.environment, | ||
awsRegion: attributes.awsRegion, | ||
correlationIds: { | ||
awsRequestId: attributes.lambdaContext?.awsRequestId, | ||
xRayTraceId: attributes.xRayTraceId, | ||
}, | ||
lambdaFunction: { | ||
name: attributes.lambdaContext?.functionName, | ||
arn: attributes.lambdaContext?.invokedFunctionArn, | ||
memoryLimitInMB: attributes.lambdaContext?.memoryLimitInMB, | ||
version: attributes.lambdaContext?.functionVersion, | ||
coldStart: attributes.lambdaContext?.coldStart, | ||
}, | ||
logLevel: attributes.logLevel, | ||
timestamp: this.formatTimestamp(attributes.timestamp), // You can extend this function | ||
logger: { | ||
sampleRateValue: attributes.sampleRateValue, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
logger.addContext(context); | ||
|
||
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } }); | ||
|
||
}; | ||
export { MyCompanyLogFormatter }; |