Skip to content

Commit 2fb3688

Browse files
shdqdreamorosi
andauthored
docs(logger): update child logger docs section and snippets (#1286)
* docs(logger): add details about child logger usage * fix: fix formatting * Update docs/core/logger.md Co-authored-by: Andrea Amorosi <[email protected]> * Update docs/core/logger.md Co-authored-by: Andrea Amorosi <[email protected]> --------- Co-authored-by: Andrea Amorosi <[email protected]>
1 parent db215dd commit 2fb3688

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Diff for: docs/core/logger.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,9 @@ The error will be logged with default key name `error`, but you can also pass yo
390390

391391
### Using multiple Logger instances across your code
392392

393-
Logger supports quick instance cloning via the `createChild` method.
394-
This can be useful for example if you want to enable multiple Loggers with different logging levels in the same Lambda invocation.
393+
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.
394+
395+
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.
395396

396397
=== "handler.ts"
397398

@@ -407,20 +408,26 @@ This can be useful for example if you want to enable multiple Loggers with diffe
407408
"message": "This is an INFO log, from the parent logger",
408409
"service": "serverlessAirline",
409410
"timestamp": "2021-12-12T22:32:54.667Z",
411+
"aws_account_id":"123456789012",
412+
"aws_region":"eu-west-1",
410413
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
411414
}
412415
{
413416
"level": "ERROR",
414417
"message": "This is an ERROR log, from the parent logger",
415418
"service": "serverlessAirline",
416419
"timestamp": "2021-12-12T22:32:54.670Z",
420+
"aws_account_id":"123456789012",
421+
"aws_region":"eu-west-1",
417422
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
418423
}
419424
{
420425
"level": "ERROR",
421426
"message": "This is an ERROR log, from the child logger",
422427
"service": "serverlessAirline",
423428
"timestamp": "2021-12-12T22:32:54.670Z",
429+
"aws_account_id":"123456789012",
430+
"aws_region":"eu-west-1",
424431
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
425432
}
426433
```
@@ -598,6 +605,9 @@ This is how the printed log would look:
598605
}
599606
```
600607

608+
!!! tip "Custom Log formatter and Child loggers"
609+
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.
610+
601611
## Testing your code
602612

603613
### Inject Lambda Context

Diff for: docs/snippets/logger/createChild.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
22

3-
// With this logger, all the INFO logs will be printed
3+
// This logger has a service name, some persistent attributes
4+
// and log level set to INFO
45
const logger = new Logger({
5-
logLevel: 'INFO'
6+
serviceName: 'serverlessAirline',
7+
logLevel: 'INFO',
8+
persistentLogAttributes: {
9+
aws_account_id: '123456789012',
10+
aws_region: 'eu-west-1',
11+
},
612
});
713

8-
// With this logger, only the ERROR logs will be printed
14+
// This other logger inherits all the parent's attributes
15+
// but the log level, which is now set to ERROR
916
const childLogger = logger.createChild({
1017
logLevel: 'ERROR'
1118
});

0 commit comments

Comments
 (0)