diff --git a/docs/core/logger.md b/docs/core/logger.md index c05fad22f0..fb25804135 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -390,8 +390,9 @@ The error will be logged with default key name `error`, but you can also pass yo ### Using multiple Logger instances across your code -Logger supports quick instance cloning via the `createChild` method. -This can be useful for example if you want to enable multiple Loggers with different logging levels in the same Lambda invocation. +The `createChild` method allows you to create a child instance of the Logger, which inherits all of the attributes from its parent. You have the option to override any of the settings and attributes from the parent logger, including [its settings](#utility-settings), any [persistent attributes](#appending-persistent-additional-log-keys-and-values), and [the log formatter](#custom-log-formatter-bring-your-own-formatter). Once a child logger is created, the logger and its parent will act as separate instances of the Logger class, and as such any change to one won't be applied to the other. + + The following example shows how to create multiple Loggers that share service name and persistent attributes while specifying different logging levels within a single Lambda invocation. As the result, only ERROR logs with all the inherited attributes will be displayed in CloudWatch Logs from the child logger, but all logs emitted will have the same service name and persistent attributes. === "handler.ts" @@ -407,6 +408,8 @@ This can be useful for example if you want to enable multiple Loggers with diffe "message": "This is an INFO log, from the parent logger", "service": "serverlessAirline", "timestamp": "2021-12-12T22:32:54.667Z", + "aws_account_id":"123456789012", + "aws_region":"eu-west-1", "xray_trace_id": "abcdef123456abcdef123456abcdef123456" } { @@ -414,6 +417,8 @@ This can be useful for example if you want to enable multiple Loggers with diffe "message": "This is an ERROR log, from the parent logger", "service": "serverlessAirline", "timestamp": "2021-12-12T22:32:54.670Z", + "aws_account_id":"123456789012", + "aws_region":"eu-west-1", "xray_trace_id": "abcdef123456abcdef123456abcdef123456" } { @@ -421,6 +426,8 @@ This can be useful for example if you want to enable multiple Loggers with diffe "message": "This is an ERROR log, from the child logger", "service": "serverlessAirline", "timestamp": "2021-12-12T22:32:54.670Z", + "aws_account_id":"123456789012", + "aws_region":"eu-west-1", "xray_trace_id": "abcdef123456abcdef123456abcdef123456" } ``` @@ -598,6 +605,9 @@ This is how the printed log would look: } ``` +!!! tip "Custom Log formatter and Child loggers" + It is not necessary to pass the `LogFormatter` each time a [child logger](#using-multiple-logger-instances-across-your-code) is created. The parent's LogFormatter will be inherited by the child logger. + ## Testing your code ### Inject Lambda Context diff --git a/docs/snippets/logger/createChild.ts b/docs/snippets/logger/createChild.ts index 7b9e7780e3..3535c03336 100644 --- a/docs/snippets/logger/createChild.ts +++ b/docs/snippets/logger/createChild.ts @@ -1,11 +1,18 @@ import { Logger } from '@aws-lambda-powertools/logger'; -// With this logger, all the INFO logs will be printed +// This logger has a service name, some persistent attributes +// and log level set to INFO const logger = new Logger({ - logLevel: 'INFO' + serviceName: 'serverlessAirline', + logLevel: 'INFO', + persistentLogAttributes: { + aws_account_id: '123456789012', + aws_region: 'eu-west-1', + }, }); -// With this logger, only the ERROR logs will be printed +// This other logger inherits all the parent's attributes +// but the log level, which is now set to ERROR const childLogger = logger.createChild({ logLevel: 'ERROR' });