From 61cc27ac13d79e6efd0e21af7ae9d8c057d9bdbd Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Mon, 13 Feb 2023 15:36:29 +0400 Subject: [PATCH 1/4] docs(logger): add details about child logger usage --- docs/core/logger.md | 11 +++++++++-- docs/snippets/logger/createChild.ts | 13 ++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/core/logger.md b/docs/core/logger.md index c05fad22f0..67cb42e401 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 of the Logger 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 that can be set through the constructor, including [the default settings](#utility-settings), any [persistent attributes](#appending-persistent-additional-log-keys-and-values), and [the log formatter](#custom-log-formatter-bring-your-own-formatter). + +The following example shows how to activate multiple Loggers and inherit the 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. === "handler.ts" @@ -407,6 +408,7 @@ 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 +416,7 @@ 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 +424,7 @@ 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 +602,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' }); From 6c587f08b65891e5d8ac12fdfbcc29668e12b519 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Mon, 13 Feb 2023 15:42:18 +0400 Subject: [PATCH 2/4] fix: fix formatting --- docs/core/logger.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/core/logger.md b/docs/core/logger.md index 67cb42e401..fedb451406 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -408,7 +408,8 @@ The following example shows how to activate multiple Loggers and inherit the ser "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", + "aws_account_id":"123456789012", + "aws_region":"eu-west-1", "xray_trace_id": "abcdef123456abcdef123456abcdef123456" } { @@ -416,7 +417,8 @@ The following example shows how to activate multiple Loggers and inherit the ser "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", + "aws_account_id":"123456789012", + "aws_region":"eu-west-1", "xray_trace_id": "abcdef123456abcdef123456abcdef123456" } { @@ -424,7 +426,8 @@ The following example shows how to activate multiple Loggers and inherit the ser "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", + "aws_account_id":"123456789012", + "aws_region":"eu-west-1", "xray_trace_id": "abcdef123456abcdef123456abcdef123456" } ``` From f31a3f0d02db907f053994e3860a513c68d61642 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Mon, 13 Feb 2023 16:23:36 +0300 Subject: [PATCH 3/4] Update docs/core/logger.md Co-authored-by: Andrea Amorosi --- docs/core/logger.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/logger.md b/docs/core/logger.md index fedb451406..9d02d2c1f7 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -390,7 +390,7 @@ The error will be logged with default key name `error`, but you can also pass yo ### Using multiple Logger instances across your code -The `createChild` method of the Logger 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 that can be set through the constructor, including [the default settings](#utility-settings), any [persistent attributes](#appending-persistent-additional-log-keys-and-values), and [the log formatter](#custom-log-formatter-bring-your-own-formatter). +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 activate multiple Loggers and inherit the 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. From f924d1ccbae1de8b74d89c0f418132d486bbd567 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Mon, 13 Feb 2023 16:25:27 +0300 Subject: [PATCH 4/4] Update docs/core/logger.md Co-authored-by: Andrea Amorosi --- docs/core/logger.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/logger.md b/docs/core/logger.md index 9d02d2c1f7..fb25804135 100644 --- a/docs/core/logger.md +++ b/docs/core/logger.md @@ -392,7 +392,7 @@ The error will be logged with default key name `error`, but you can also pass yo 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 activate multiple Loggers and inherit the 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. + 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"