Skip to content

Commit 5eadaef

Browse files
docs(logger): add logEventIfEnabled() docs (#2924)
Co-authored-by: Leandro Damascena <[email protected]>
1 parent 826f1f1 commit 5eadaef

File tree

8 files changed

+90
-32
lines changed

8 files changed

+90
-32
lines changed

Diff for: docs/core/logger.md

+30-8
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,48 @@ In each case, the printed log will look like this:
160160

161161
### Log incoming event
162162

163-
When debugging in non-production environments, you can instruct Logger to log the incoming event with the middleware/decorator parameter `logEvent`.
163+
When debugging in non-production environments, you can log the incoming event using the `logEventIfEnabled()` method or by setting the `logEvent` option in the `injectLambdaContext()` Middy.js middleware or class method decorator.
164164

165165
???+ warning
166166
This is disabled by default to prevent sensitive info being logged
167167

168-
=== "Middy Middleware"
168+
=== "`logEventIfEnabled()`"
169+
170+
```typescript hl_lines="1 8"
171+
--8<-- "examples/snippets/logger/logEventManual.ts"
172+
```
173+
174+
1. You can control the event logging via the `POWERTOOLS_LOGGER_LOG_EVENT` environment variable.
169175

170-
```typescript hl_lines="15"
171-
--8<-- "examples/snippets/logger/eventMiddy.ts"
176+
=== "Middy.js Middleware"
177+
178+
```typescript hl_lines="10"
179+
--8<-- "examples/snippets/logger/logEventMiddy.ts"
172180
```
173181

182+
1. The `logEvent` option takes precedence over the `POWERTOOLS_LOGGER_LOG_EVENT` environment variable.
183+
174184
=== "Decorator"
175185

176-
```typescript hl_lines="8"
177-
--8<-- "examples/snippets/logger/eventDecorator.ts"
186+
```typescript hl_lines="7"
187+
--8<-- "examples/snippets/logger/logEventDecorator.ts"
178188
```
179189

180-
1. Binding your handler method allows your handler to access `this` within the class methods.
190+
1. The `logEvent` option takes precedence over the `POWERTOOLS_LOGGER_LOG_EVENT` environment variable.
191+
192+
=== "payload.json"
193+
194+
```json
195+
--8<-- "examples/snippets/logger/samples/logEventInput.json"
196+
```
197+
198+
=== "CloudWatch output"
199+
200+
```json hl_lines="8 13-15"
201+
--8<-- "examples/snippets/logger/samples/logEventOutput.json"
202+
```
181203

182-
Use `POWERTOOLS_LOGGER_LOG_EVENT` environment variable to enable or disable (`true`/`false`) this feature.
204+
Use `POWERTOOLS_LOGGER_LOG_EVENT` environment variable to enable or disable (`true`/`false`) this feature. When using Middy.js middleware or class method decorator, the `logEvent` option will take precedence over the environment variable.
183205

184206
### Appending additional keys
185207

Diff for: examples/snippets/logger/eventMiddy.ts

-16
This file was deleted.

Diff for: examples/snippets/logger/eventDecorator.ts renamed to examples/snippets/logger/logEventDecorator.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { Logger } from '@aws-lambda-powertools/logger';
44
const logger = new Logger();
55

66
class Lambda implements LambdaInterface {
7-
// Set the log event flag to true
8-
@logger.injectLambdaContext({ logEvent: true })
7+
@logger.injectLambdaContext({ logEvent: true }) // (1)
98
public async handler(_event: unknown, _context: unknown): Promise<void> {
10-
logger.info('This is an INFO log with some context');
9+
// ... your logic here
1110
}
1211
}
1312

1413
const myFunction = new Lambda();
15-
export const handler = myFunction.handler.bind(myFunction); // (1)
14+
export const handler = myFunction.handler.bind(myFunction);

Diff for: examples/snippets/logger/logEventManual.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
process.env.POWERTOOLS_LOGGER_LOG_EVENT = 'true';
2+
3+
import { Logger } from '@aws-lambda-powertools/logger';
4+
5+
const logger = new Logger();
6+
7+
export const handler = async (event: unknown) => {
8+
logger.logEventIfEnabled(event); // (1)
9+
// ... your logic here
10+
};

Diff for: examples/snippets/logger/logEventMiddy.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
3+
import middy from '@middy/core';
4+
5+
const logger = new Logger();
6+
7+
export const handler = middy(async () => {
8+
// ... your logic here
9+
}).use(
10+
injectLambdaContext(logger, { logEvent: true }) // (1)
11+
);

Diff for: examples/snippets/logger/samples/logEventInput.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}

Diff for: examples/snippets/logger/samples/logEventOutput.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"cold_start": true,
3+
"function_arn": "arn:aws:lambda:eu-west-1:123456789012:function:LogEventFn",
4+
"function_memory_size": "128",
5+
"function_name": "LogEventFn",
6+
"function_request_id": "0a9df60d-e2de-447d-ba3e-45f149eae6c9",
7+
"level": "INFO",
8+
"message": "Lambda invocation event",
9+
"sampling_rate": 0,
10+
"service": "service_undefined",
11+
"timestamp": "2024-08-14T10:08:06.199Z",
12+
"xray_trace_id": "1-66bc8205-21f8b5190da519d22b2b0533",
13+
"event": {
14+
"foo": "bar"
15+
}
16+
}

Diff for: packages/logger/src/Logger.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,24 @@ class Logger extends Utility implements LoggerInterface {
498498
}
499499

500500
/**
501-
* Logs a Lambda invocation event, if it *should*.
501+
* Log the AWS Lambda event payload for the current invocation if the environment variable `POWERTOOLS_LOG_EVENT` is set to `true`.
502502
*
503-
** @param {unknown} event
504-
* @param {boolean} [overwriteValue]
505-
* @returns {void}
503+
* @example
504+
* ```ts
505+
* process.env.POWERTOOLS_LOG_EVENT = 'true';
506+
*
507+
* import { Logger } from '@aws-lambda-powertools/logger';
508+
*
509+
* const logger = new Logger();
510+
*
511+
* export const handler = async (event) => {
512+
* logger.logEventIfEnabled(event);
513+
* // ... your handler code
514+
* }
515+
* ```
516+
*
517+
* @param {unknown} event - The AWS Lambda event payload.
518+
* @param {boolean} overwriteValue - Overwrite the environment variable value.
506519
*/
507520
public logEventIfEnabled(event: unknown, overwriteValue?: boolean): void {
508521
if (!this.shouldLogEvent(overwriteValue)) return;

0 commit comments

Comments
 (0)