Skip to content

Commit ee2abb5

Browse files
committed
docs(logger): add logEventIfEnabled() docs
1 parent 47f0161 commit ee2abb5

File tree

8 files changed

+89
-32
lines changed

8 files changed

+89
-32
lines changed

docs/core/logger.md

Lines changed: 30 additions & 8 deletions
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 7"
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

examples/snippets/logger/eventMiddy.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/snippets/logger/eventDecorator.ts renamed to examples/snippets/logger/logEventDecorator.ts

Lines changed: 3 additions & 4 deletions
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 lambda handler
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);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
process.env.POWERTOOLS_LOG_EVENT = 'true';
2+
3+
import { Logger } from '@aws-lambda-powertools/logger';
4+
const logger = new Logger();
5+
6+
export const handler = async (event: unknown) => {
7+
logger.logEventIfEnabled(event); // (1)
8+
// ... your handler code
9+
};
Lines changed: 11 additions & 0 deletions
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 lambda handler
9+
}).use(
10+
injectLambdaContext(logger, { logEvent: true }) // (1)
11+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}
Lines changed: 16 additions & 0 deletions
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+
}

packages/logger/src/Logger.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,24 @@ class Logger extends Utility implements LoggerInterface {
497497
}
498498

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

0 commit comments

Comments
 (0)