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
createCreates a new Logger instance.
Optional
options: ConstructorOptionsIt 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: HandlerOptionsimport { 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);
https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
Protected
isIf 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).
Set the log level for this Logger instance.
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.
Protected
shouldProtected
Decides whether the current log item should be printed or not.
The decision is based on the log level and the sample rate value. A log item will be printed if:
true
.It prints a log item with level WARN.
Rest
...extraInput: LogItemExtraInputStatic
injectOptional
options: HandlerOptionsStatic
injectOptional
options: HandlerOptionsGenerated 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/