Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): logger bringYourOwnFormatter snippet #1254

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 34 additions & 21 deletions docs/snippets/logger/bringYourOwnFormatterClass.ts
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 };