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.
Add the given persistent attributes (key-value pairs) to all log items generated by this Logger instance.
The attributes to add to all log items.
This method is deprecated and will be removed in the future major versions, please use () instead.
Add the given temporary attributes (key-value pairs) to all log items generated by this Logger instance.
The attributes to add to all log items.
Add the given persistent attributes (key-value pairs) to all log items generated by this Logger instance.
The attributes to add to all log items.
Protected
createCreate a log item and populate it with the given log level, input, and extra input.
We start with creating an object with base attributes managed by Powertools.
Then we create a second object with persistent attributes provided by customers either
directly to the log entry or through initial configuration and appendKeys
method.
Once we have the two objects, we pass them to the formatter that will apply the desired formatting to the log item.
The log level of the log item to be printed
The main input of the log item, this can be a string or an object with additional attributes
Additional attributes to be added to the log item
Create a separate Logger instance, identical to the current one. It's possible to overwrite the new instance options by passing them.
The options to initialize the child logger with.
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.
// 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;
}
}
Print a log item with level CRITICAL.
The log message.
The extra input to log.
Print a log item with level DEBUG.
The extra input to log.
Print a log item with level ERROR.
The log message.
The extra input to log.
Protected
getProtected
getA custom JSON replacer function that is used to serialize the log items.
By default, we already extend the default serialization behavior to handle BigInt
and Error
objects, as well as remove circular references.
When a custom JSON replacer function is passed to the Logger constructor, it will be called before our custom rules for each key-value pair in the object being stringified.
This allows you to customize the serialization while still benefiting from the default behavior.
Get the log level name of the current instance of Logger.
Returns the log level name, i.e. INFO
, DEBUG
, etc.
To get the log level as a number, use the Logger.level property.
Return the persistent log attributes, which are the attributes that will be logged in all log items.
Print a log item with level INFO.
The log message.
The extra input to log.
Class method decorator that adds the current Lambda function context as extra information in all log items.
This decorator is useful when you want to add the Lambda context to all log items and it works only when decorating a class method that is a Lambda function handler.
Optional
options: InjectLambdaContextOptionsimport { Logger } from '@aws-lambda-powertools/logger';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
const logger = new Logger({ serviceName: 'serverlessAirline' });
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);
Protected
isLog the AWS Lambda event payload for the current invocation if the environment variable POWERTOOLS_LOGGER_LOG_EVENT
is set to true
.
The AWS Lambda event payload.
Optional
overwriteValue: booleanOverwrite the environment variable value.
Protected
printProtected
processPrint a given log with given log level.
The log level threshold
The log message
The extra input to log
The keys to remove.
This method is deprecated and will be removed in the future major versions. Use () instead.
Set 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.
Set the given attributes (key-value pairs) to all log items generated by this Logger instance. Note: this replaces the pre-existing value.
The attributes to set.
This method is deprecated and will be removed in the future major versions, please use () instead.
Print a log item with level TRACE.
The log message.
The extra input to log.
Print a log item with level WARN.
The log message.
The extra input to log.
Static
injectOptional
options: InjectLambdaContextOptionsThis method is deprecated and will be removed in the future major versions. Use () instead.
Static
injectOptional
options: InjectLambdaContextOptions
The Logger utility provides an opinionated logger with output structured as JSON for AWS Lambda.
Key features
DEBUG
based on a sample rate value for a percentage of invocationsAfter initializing the Logger class, you can use the methods to log messages at different levels.
Example
To enrich the log items with information from the Lambda context, you can use the addContext() method.
Example
You can also add additional attributes to all log items using the appendKeys() method.
Example
If you write your functions as classes and use TypeScript, you can use the Logger.injectLambdaContext class method decorator to automatically add context to your logs and clear the state after the invocation.
If instead you use Middy.js middlewares, you use the injectLambdaContext() middleware.
See
https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/