Standardize logs with AWS Lambda Powertools

Standardize your function logs with Powertools.

Cloudwatch Log Insights snippet that returns logs that are not exceptions

Select language:

Using Powertools Logger with TypeScript:

Install the library in your project:

The Logger utility must always be instantiated outside the Lambda handler. By doing this, subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time. In addition, Logger can keep track of a cold start and inject the appropriate fields into logs.

npm install @aws-lambda-powertools/logger
                                                      


The Logger utility must always be instantiated outside the Lambda handler. By doing this, subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time. In addition, Logger can keep track of a cold start and inject the appropriate fields into logs.

import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
import middy from '@middy/core';

const logger = new Logger({
  logLevel: 'INFO',
  serviceName: 'shopping-cart-api',
});

const lambdaHandler = async (): Promise<void> => {
  logger.info('This is an INFO log with some context');
};

export const handler = middy(lambdaHandler).use(injectLambdaContext(logger));

                                                      



Created by:

David Boyne

David Boyne

Helping people learn about event-driven architectures and serverless

Follow on LinkedIn

James Eastham

James Eastham

I am a Cloud Infrastructure Architect with AWS Professional Services.

Rahul Popat

Rahul Popat

Senior Specialist Solutions Architect at AWS

Follow on LinkedIn