It initializes the Logger class with an optional set of options (settings). *
Log level used by the current instance of Logger.
Returns the log level as a number. The higher the number, the less verbose the logs. To get the log level name, use the () method.
It adds the given attributes (key-value pairs) to all log items generated by this Logger instance.
Optional
attributes: LogAttributesAlias for addPersistentLogAttributes.
Optional
attributes: LogAttributesIt creates a separate Logger instance, identical to the current one It's possible to overwrite the new instance options by passing them.
Protected
createFactory method for instantiating logger instances. Used by createChild
method.
Important for customization and subclassing. It allows subclasses, like MyOwnLogger
,
to override its behavior while keeping the main business logic in createChild
intact.
Optional
options: ConstructorOptionsLogger configuration options.
A new logger instance.
// MyOwnLogger subclass
class MyOwnLogger extends Logger {
protected createLogger(options?: ConstructorOptions): MyOwnLogger {
return new MyOwnLogger(options);
}
// No need to re-implement business logic from `createChild` and keep track on changes
public createChild(options?: ConstructorOptions): MyOwnLogger {
return super.createChild(options) as MyOwnLogger;
}
}
It prints a log item with level CRITICAL.
Rest
...extraInput: LogItemExtraInputIt prints a log item with level DEBUG.
Rest
...extraInput: LogItemExtraInputIt prints a log item with level ERROR.
Rest
...extraInput: LogItemExtraInputProtected
getGet the log level name of the current instance of Logger.
It returns the log level name, i.e. INFO
, DEBUG
, etc.
To get the log level as a number, use the Logger.level property.
The log level name.
It prints a log item with level INFO.
Rest
...extraInput: LogItemExtraInputMethod decorator that adds the current Lambda function context as extra information in all log items.
The decorator can be used only when attached to a Lambda function handler which is written as method of a class, and should be declared just before the handler declaration.
Note: Currently TypeScript only supports decorators on classes and methods. If you are using the function syntax, you should use the middleware instead.
Optional
options: InjectLambdaContextOptionsimport { Logger } from '@aws-lambda-powertools/logger';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
const logger = new Logger();
class Lambda implements LambdaInterface {
// Decorate your handler class method
@logger.injectLambdaContext()
public async handler(_event: unknown, _context: unknown): Promise<void> {
logger.info('This is an INFO log with some context');
}
}
const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);
https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
Protected
isSet the log level for this Logger instance.
If the log level is set using AWS Lambda Advanced Logging Controls, it sets it instead of the given log level to avoid data loss.
The log level to set, i.e. error
, warn
, info
, debug
, etc.
It sets the given attributes (key-value pairs) to all log items generated by this Logger instance. Note: this replaces the pre-existing value.
It prints a log item with level WARN.
Rest
...extraInput: LogItemExtraInputStatic
injectOptional
options: InjectLambdaContextOptionsStatic
injectOptional
options: InjectLambdaContextOptionsGenerated using TypeDoc
Intro
The Logger utility provides an opinionated logger with output structured as JSON.
Key features
Usage
For more usage examples, see our documentation.
Basic usage
Example
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
Object oriented usage with decorators
If instead you use TypeScript classes to wrap your Lambda handler you can use the @logger.injectLambdaContext() decorator.
Example
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
Implements
See
https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/