From fdbba32d8b3545730d242ac4fd1ef2d83cdbccce Mon Sep 17 00:00:00 2001 From: Niko Achilles Kokkinos Date: Tue, 17 Jan 2023 15:29:22 +0200 Subject: [PATCH] fix(docs): logger bringYourOwnFormatter snippet #1253 (#1254) --- .../logger/bringYourOwnFormatterClass.ts | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/docs/snippets/logger/bringYourOwnFormatterClass.ts b/docs/snippets/logger/bringYourOwnFormatterClass.ts index 0121e1bc64..b294ee0dc5 100644 --- a/docs/snippets/logger/bringYourOwnFormatterClass.ts +++ b/docs/snippets/logger/bringYourOwnFormatterClass.ts @@ -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 => { +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' } }); - -}; \ No newline at end of file +export { MyCompanyLogFormatter };