You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/logger.md
+152-102
Original file line number
Diff line number
Diff line change
@@ -181,93 +181,199 @@ When debugging in non-production environments, you can instruct Logger to log th
181
181
182
182
Use `POWERTOOLS_LOGGER_LOG_EVENT` environment variable to enable or disable (`true`/`false`) this feature.
183
183
184
-
### Appending persistent additional log keys and values
184
+
### Appending additional keys
185
185
186
-
You can append additional persistent keys and values in the logs generated during a Lambda invocation using either mechanism:
186
+
You can append additional keys using either machanism:
187
187
188
-
* Via the Logger's `appendKeys` method, for all log items generated after calling this method
189
-
* Passing them in the Logger's constructor
188
+
* Add **extra keys** to a single log message by passing them to the log method directly
189
+
* Append **temporary keys** to all future log messages via the `appendKeys()` method until `resetKeys()` is called
190
+
* Set **Persistent keys** for the logger instance via the `persistentKeys` constructor option or the `appendPersistentKeys()` method
190
191
191
-
To remove the keys you added, you can use the `removeKeys` method.
192
+
#### Extra keys
193
+
194
+
You can append additional data to a single log item by passing objects as additional parameters.
195
+
196
+
* Pass a simple string for logging it with default key name `extra`
197
+
* Pass one or multiple objects containing arbitrary data to be logged. Each data object should be placed in an enclosing object as a single property value, you can name this property as you need: `{ myData: arbitraryObjectToLog }`
198
+
* If you already have an object containing a `message` key and an additional property, you can pass this object directly
192
199
193
200
=== "handler.ts"
194
201
195
-
```typescript hl_lines="5-13 17-25 32"
196
-
--8<-- "examples/snippets/logger/appendKeys.ts"
202
+
```typescript hl_lines="16-18 23-25 37"
203
+
--8<-- "examples/snippets/logger/extraData.ts"
197
204
```
198
205
=== "Example CloudWatch Logs excerpt"
199
206
200
-
```json hl_lines="7-12 20-25"
207
+
```json hl_lines="7 15-21 29 37"
201
208
{
202
209
"level": "INFO",
203
-
"message": "This is an INFO log",
210
+
"message": "This is a log with an extra variable",
You can persist keys across Lambda invocations by using the `persistentKeys` constructor option or the `appendPersistentKeys()` method. These keys will persist even if you call the [`resetKeys()` method](#resetting-keys).
286
+
287
+
A common use case is to set keys about your environment or application version, so that you can easily filter logs in CloudWatch Logs.
!!! tip "Logger will automatically ignore any key with an `undefined` value"
315
+
### Removing additional keys
316
+
317
+
You can remove additional keys from the logger instance at any time:
318
+
319
+
* Remove temporary keys added via the `appendKeys()` method by using the `removeKeys()` method
320
+
* Remove persistent keys added via the `persistentKeys` constructor option or the `appendPersistentKeys()` method by using the `removePersistentKeys()` method
Logger is commonly initialized in the global scope. Due to [Lambda Execution Context](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html){target="_blank"} reuse, this means that custom keys can be persisted across invocations.
337
+
338
+
Resetting the state allows you to clear all the temporary keys you have added.
339
+
340
+
???+ tip "Tip: When is this useful?"
341
+
This is useful when you add multiple custom keys conditionally or when you use canonical or wide logs.
232
342
233
-
The Logger utility is commonly initialized in the global scope, outside the handler function.
234
-
When you attach persistent log attributes through the `persistentLogAttributes` constructor option or via the `appendKeys`, `addPersistentLogAttributes` methods, this data is attached to the Logger instance.
343
+
=== "Clearing state manually"
235
344
236
-
Due to the [Lambda Execution Context reuse](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html), this means those persistent log attributes may be reused across invocations.
237
-
If you want to make sure that persistent attributes added **inside the handler function** code are not persisted across invocations, you can set the parameter `clearState` as `true` in the `injectLambdaContext` middleware or decorator.
### Appending additional data to a single log item
296
-
297
-
You can append additional data to a single log item by passing objects as additional parameters.
298
-
299
-
* Pass a simple string for logging it with default key name `extra`
300
-
* Pass one or multiple objects containing arbitrary data to be logged. Each data object should be placed in an enclosing object as a single property value, you can name this property as you need: `{ myData: arbitraryObjectToLog }`
301
-
* If you already have an object containing a `message` key and an additional property, you can pass this object directly
302
-
303
-
=== "handler.ts"
304
-
305
-
```typescript hl_lines="16-18 23-25 37"
306
-
--8<-- "examples/snippets/logger/extraData.ts"
307
-
```
308
-
=== "Example CloudWatch Logs excerpt"
309
-
310
-
```json hl_lines="7 15-21 29 37"
311
-
{
312
-
"level": "INFO",
313
-
"message": "This is a log with an extra variable",
You can log errors by using the `error` method and pass the error object as parameter.
@@ -471,7 +521,7 @@ In the event you have set a log level in Powertools to a level that is lower tha
471
521
472
522
### Using multiple Logger instances across your code
473
523
474
-
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).
524
+
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 [extra keys](#appending-additional-keys), and [the log formatter](#custom-log-formatter-bring-your-own-formatter).
475
525
476
526
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.
0 commit comments