Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(logger): update child logger docs section and snippets #1286

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -407,20 +408,26 @@ 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"
}
{
"level": "ERROR",
"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"
}
{
"level": "ERROR",
"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"
}
```
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions docs/snippets/logger/createChild.ts
Original file line number Diff line number Diff line change
@@ -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'
});
Expand Down