Skip to content

Commit 9c97124

Browse files
author
Dmitry Balabanov
committed
docs: update docs about logging additional keys
1 parent 2992b28 commit 9c97124

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

docs/core/logger.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,12 @@ You can append additional persistent keys and values in the logs generated durin
276276
### Appending additional log keys and values to a single log item
277277

278278
You can append additional keys and values in a single log item passing them as parameters.
279+
Pass a string for logging it with default key name `extra`. Alternatively, pass one or multiple objects with custom keys.
280+
If you already have an object containing a `message` key and an additional property, you can pass this object directly.
279281

280282
=== "handler.ts"
281283

282-
```typescript hl_lines="14 18-19"
284+
```typescript hl_lines="14 18-19 23 31"
283285
import { Logger } from '@aws-lambda-powertools/logger';
284286

285287
const logger = new Logger();
@@ -300,6 +302,17 @@ You can append additional keys and values in a single log item passing them as p
300302
{ data: myImportantVariable },
301303
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } }
302304
);
305+
306+
// Simply pass a string for logging additional data
307+
logger.info('This is a log with additional string value', 'string value');
308+
309+
// Directly passing an object containing both the message and the additional info
310+
const logObject = {
311+
message: 'This is a log message',
312+
additionalValue: 42
313+
};
314+
315+
logger.info(logObject);
303316
304317
return {
305318
foo: 'bar'
@@ -309,7 +322,7 @@ You can append additional keys and values in a single log item passing them as p
309322
```
310323
=== "Example CloudWatch Logs excerpt"
311324

312-
```json hl_lines="7 15-16"
325+
```json hl_lines="7 15-16 24 32"
313326
{
314327
"level": "INFO",
315328
"message": "This is a log with an extra variable",
@@ -327,6 +340,22 @@ You can append additional keys and values in a single log item passing them as p
327340
"data": { "foo": "bar" },
328341
"correlationIds": { "myCustomCorrelationId": "foo-bar-baz" }
329342
}
343+
{
344+
"level": "INFO",
345+
"message": "This is a log with additional string value",
346+
"service": "serverlessAirline",
347+
"timestamp": "2021-12-12T22:06:17.463Z",
348+
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
349+
"extra": "string value"
350+
}
351+
{
352+
"level": "INFO",
353+
"message": "This is a log message",
354+
"service": "serverlessAirline",
355+
"timestamp": "2021-12-12T22:06:17.463Z",
356+
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
357+
"additionalValue": 42
358+
}
330359
```
331360

332361
### Logging errors
@@ -347,14 +376,14 @@ The error will be logged with default key name `error`, but you can also pass yo
347376
throw new Error('Unexpected error #1');
348377
} catch (error) {
349378
// Log information about the error using the default "error" key
350-
logger.error('This is the first error', error);
379+
logger.error('This is the first error', error as Error);
351380
}
352381

353382
try {
354383
throw new Error('Unexpected error #2');
355384
} catch (error) {
356385
// Log information about the error using a custom "myCustomErrorKey" key
357-
logger.error('This is the second error', { myCustomErrorKey: error } );
386+
logger.error('This is the second error', { myCustomErrorKey: error as Error } );
358387
}
359388

360389
};
@@ -391,6 +420,9 @@ The error will be logged with default key name `error`, but you can also pass yo
391420
}
392421
```
393422

423+
!!! tip "Logging errors and log level"
424+
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.
425+
394426
## Advanced
395427

396428
### Using multiple Logger instances across your code

0 commit comments

Comments
 (0)