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
feat(logger): add clear state functionality (#902)
* feat: add business logic for clear state
* chore: removed temporary code
* feat: business logic + unit tests for decorator and middleware
* chore: format TS
* chore: typo in logger docs
* chore: lint fix quotes
* chore: typo in logger docs
* tests(logger): rename test scenario for clear state
* chore: exclude non-null rule in decorator
* build(deps): revert package-lock.json
* test(logger): clear state e2e tests
Copy file name to clipboardExpand all lines: docs/core/logger.md
+120
Original file line number
Diff line number
Diff line change
@@ -282,6 +282,125 @@ To remove the keys you added, you can use the `removeKeys` method.
282
282
283
283
!!! tip "Logger will automatically ignore any key with an `undefined` value"
284
284
285
+
#### Clearing all state
286
+
287
+
The Logger utility is commonly initialized in the global scope, outside the handler function.
288
+
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.
289
+
290
+
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.
291
+
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.
292
+
293
+
=== "Middy Middleware"
294
+
295
+
```typescript hl_lines="27"
296
+
import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
297
+
import middy from '@middy/core';
298
+
299
+
// Persistent attributes added outside the handler will be
### Appending additional data to a single log item
286
405
287
406
You can append additional data to a single log item by passing objects as additional parameters.
@@ -440,6 +559,7 @@ The error will be logged with default key name `error`, but you can also pass yo
440
559
!!! tip "Logging errors and log level"
441
560
You can also log errors using the `warn`, `info`, and `debug` methods. Be aware of the log level though, you might miss those errors when analyzing the log later depending on the log level configuration.
442
561
562
+
443
563
## Advanced
444
564
445
565
### Using multiple Logger instances across your code
0 commit comments