Options
All
  • Public
  • Public/Protected
  • All
Menu

Intro

The Logger utility provides an opinionated logger with output structured as JSON.

Key features

  • Capture key fields from Lambda context, cold start and structures logging output as JSON
  • Log Lambda context when instructed (disabled by default)
  • Log sampling prints all logs for a percentage of invocations (disabled by default)
  • Append additional keys to structured log at any point in time

Usage

For more usage examples, see our documentation.

Basic usage

example
import { Logger } from "@aws-lambda-powertools/logger";

// Logger parameters fetched from the environment variables:
const logger = new Logger();

Functions usage with manual instrumentation

If you prefer to manually instrument your Lambda handler you can use the methods in the Logger class directly.

example
import { Logger } from "@aws-lambda-powertools/logger";

const logger = new Logger();

export const handler = async (_event, context) => {
logger.addContext(context);
logger.info("This is an INFO log with some context");
};

Functions usage with middleware

If you use function-based Lambda handlers you can use the injectLambdaContext() middy middleware to automatically add context to your Lambda logs.

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

const logger = new Logger();

const lambdaHandler = async (_event: any, _context: any) => {
logger.info("This is an INFO log with some context");
};

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

Object oriented usage with decorators

If instead you use TypeScript classes to wrap your Lambda handler you can use the @logger.injectLambdaContext() decorator.

example
import { Logger } from "@aws-lambda-powertools/logger";
import { LambdaInterface } from '@aws-lambda-powertools/commons';

const logger = new Logger();

class Lambda implements LambdaInterface {
// Decorate your handler class method
@logger.injectLambdaContext()
public async handler(_event: any, _context: any): Promise<void> {
logger.info("This is an INFO log with some context");
}
}

const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);
implements

{ClassThatLogs}

see

https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/

Hierarchy

Implements

Index

Constructors

Methods

  • addContext(context: Context): void
  • It adds the current Lambda function's invocation context data to the powertoolLogData property of the instance. This context data will be part of all printed log items.

    Parameters

    • context: Context

    Returns void

  • It adds the given attributes (key-value pairs) to all log items generated by this Logger instance.

    Parameters

    Returns void

  • getColdStart(): boolean
  • getLogEvent(): boolean
  • It returns a boolean value. True means that the Lambda invocation events are printed in the logs.

    Returns boolean

  • getLogsSampled(): boolean
  • isColdStart(): boolean
  • logEventIfEnabled(event: unknown, overwriteValue?: boolean): void
  • Logs a Lambda invocation event, if it should.

    • @param {unknown} event

    Parameters

    • event: unknown
    • Optional overwriteValue: boolean

    Returns void

  • refreshSampleRateCalculation(): void
  • If the sample rate feature is enabled, the calculation that determines whether the logs will actually be printed or not for this invocation is done when the Logger class is initialized. This method will repeat that calculation (with possible different outcome).

    Returns void

  • removeKeys(keys: string[]): void
  • removePersistentLogAttributes(keys: string[]): void
  • It removes attributes based on provided keys to all log items generated by this Logger instance.

    Parameters

    • keys: string[]

    Returns void

  • It sets the given attributes (key-value pairs) to all log items generated by this Logger instance. Note: this replaces the pre-existing value.

    Parameters

    Returns void

  • setSampleRateValue(sampleRateValue?: number): void
  • It sets the user-provided sample rate value.

    Parameters

    • Optional sampleRateValue: number

    Returns void

Generated using TypeDoc