Skip to content

Commit c63fdd6

Browse files
committed
Conformed tracer & logger
1 parent b1bc7f9 commit c63fdd6

File tree

2 files changed

+168
-148
lines changed

2 files changed

+168
-148
lines changed

Diff for: docs/core/logger.md

+52-47
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ For a **complete list** of supported environment variables, refer to [this secti
5959
// You can also pass the parameters in the constructor
6060
// const logger = new Logger({
6161
// logLevel: "WARN",
62-
// serviceName: "shopping-cart-api"
62+
// serviceName: "serverlessAirline"
6363
// });
6464
```
6565

@@ -74,7 +74,7 @@ For a **complete list** of supported environment variables, refer to [this secti
7474
Environment:
7575
Variables:
7676
LOG_LEVEL: WARN
77-
POWERTOOLS_SERVICE_NAME: shopping-cart-api
77+
POWERTOOLS_SERVICE_NAME: serverlessAirline
7878
```
7979

8080
### Standard structured keys
@@ -105,7 +105,23 @@ Key | Example
105105
**function_arn**: `string` | `arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1`
106106
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e812345678`
107107

108-
=== "Middleware"
108+
=== "Manual"
109+
110+
```typescript hl_lines="7"
111+
import { Logger } from "@aws-lambda-powertools/logger";
112+
113+
const logger = new Logger();
114+
115+
export const handler = async (_event, context) => {
116+
117+
logger.addContext(context);
118+
119+
logger.info("This is an INFO log with some context");
120+
121+
};
122+
```
123+
124+
=== "Middy Middleware"
109125

110126
!!! note
111127
Middy comes bundled with Logger, so you can just import it when using the middleware.
@@ -119,30 +135,14 @@ Key | Example
119135

120136
const logger = new Logger();
121137

122-
const lambdaHandler = async () => {
138+
const lambdaHandler = async (_event: any, _context: any) => {
123139
logger.info("This is an INFO log with some context");
124140
};
125141

126-
const handler = middy(lambdaHandler)
142+
export const handler = middy(lambdaHandler)
127143
.use(injectLambdaContext(logger));
128144
```
129145

130-
=== "Manual"
131-
132-
```typescript hl_lines="7"
133-
import { Logger } from "@aws-lambda-powertools/logger";
134-
135-
const logger = new Logger();
136-
137-
const lambdaHandler = async (_event, context) => {
138-
139-
logger.addContext(context);
140-
141-
logger.info("This is an INFO log with some context");
142-
143-
};
144-
```
145-
146146
=== "Decorator"
147147

148148
```typescript hl_lines="7"
@@ -151,13 +151,16 @@ Key | Example
151151
const logger = new Logger();
152152

153153
class Lambda {
154-
154+
// Decorate your handler class method
155155
@logger.injectLambdaContext()
156-
public handler() {
156+
public handler(_event: any, _context: any) {
157157
logger.info("This is an INFO log with some context");
158158
}
159159

160160
}
161+
162+
export const handlerClass = new Lambda();
163+
export const handler = handlerClass.handler;
161164
```
162165

163166
In each case, the printed log will look like this:
@@ -173,7 +176,7 @@ In each case, the printed log will look like this:
173176
"function_name": "shopping-cart-api-lambda-prod-eu-central-1",
174177
"level": "INFO",
175178
"message": "This is an INFO log with some context",
176-
"service": "shopping-cart-api",
179+
"service": "serverlessAirline",
177180
"timestamp": "2021-12-12T21:21:08.921Z",
178181
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
179182
}
@@ -213,7 +216,7 @@ You can append additional persistent keys and values in the logs generated durin
213216
// }
214217
// });
215218

216-
const lambdaHandler: Handler = async () => {
219+
export const handler = async (_event: any, _context: any) => {
217220

218221
// This info log will print all extra custom attributes added above
219222
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
@@ -232,7 +235,7 @@ You can append additional persistent keys and values in the logs generated durin
232235
{
233236
"level": "INFO",
234237
"message": "This is an INFO log",
235-
"service": "shopping-cart-api",
238+
"service": "serverlessAirline",
236239
"timestamp": "2021-12-12T21:49:58.084Z",
237240
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
238241
"aws_account_id": "123456789012",
@@ -245,7 +248,7 @@ You can append additional persistent keys and values in the logs generated durin
245248
{
246249
"level": "INFO",
247250
"message": "This is another INFO log",
248-
"service": "shopping-cart-api",
251+
"service": "serverlessAirline",
249252
"timestamp": "2021-12-12T21:49:58.088Z",
250253
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
251254
"aws_account_id": "123456789012",
@@ -271,7 +274,7 @@ You can append additional keys and values in a single log item passing them as p
271274

272275
const logger = new Logger();
273276

274-
const lambdaHandler = async () => {
277+
export const handler = async (_event: any, _context: any) => {
275278

276279
const myImportantVariable = {
277280
foo: "bar"
@@ -300,15 +303,15 @@ You can append additional keys and values in a single log item passing them as p
300303
{
301304
"level": "INFO",
302305
"message": "This is a log with an extra variable",
303-
"service": "shopping-cart-api",
306+
"service": "serverlessAirline",
304307
"timestamp": "2021-12-12T22:06:17.463Z",
305308
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
306309
"data": { foo: "bar" }
307310
}
308311
{
309312
"level": "INFO",
310313
"message": "This is a log with 2 extra variables",
311-
"service": "shopping-cart-api",
314+
"service": "serverlessAirline",
312315
"timestamp": "2021-12-12T22:06:17.466Z",
313316
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
314317
"data": { "foo": "bar" },
@@ -328,7 +331,7 @@ The error will be logged with default key name `error`, but you can also pass yo
328331

329332
const logger = new Logger();
330333

331-
const lambdaHandler = async () => {
334+
export const handler = async (_event: any, _context: any) => {
332335

333336
try {
334337
throw new Error("Unexpected error #1");
@@ -353,7 +356,7 @@ The error will be logged with default key name `error`, but you can also pass yo
353356
{
354357
"level": "ERROR",
355358
"message": "This is an ERROR log #1",
356-
"service": "shopping-cart-api",
359+
"service": "serverlessAirline",
357360
"timestamp": "2021-12-12T22:12:39.345Z",
358361
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
359362
"error": {
@@ -366,7 +369,7 @@ The error will be logged with default key name `error`, but you can also pass yo
366369
{
367370
"level": "ERROR",
368371
"message": "This is an ERROR log #2",
369-
"service": "shopping-cart-api",
372+
"service": "serverlessAirline",
370373
"timestamp": "2021-12-12T22:12:39.377Z",
371374
"xray_trace_id": "abcdef123456abcdef123456abcdef123456",
372375
"myCustomErrorKey": {
@@ -400,7 +403,7 @@ This can be useful for example if you want to enable multiple Loggers with diffe
400403
logLevel: "ERROR"
401404
});
402405

403-
const lambdaHandler: Handler = async () => {
406+
export const handler = async (_event: any, _context: any) => {
404407

405408
logger.info("This is an INFO log, from the parent logger");
406409
logger.error("This is an ERROR log, from the parent logger");
@@ -417,21 +420,21 @@ This can be useful for example if you want to enable multiple Loggers with diffe
417420
{
418421
"level": "INFO",
419422
"message": "This is an INFO log, from the parent logger",
420-
"service": "shopping-cart-api",
423+
"service": "serverlessAirline",
421424
"timestamp": "2021-12-12T22:32:54.667Z",
422425
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
423426
}
424427
{
425428
"level": "ERROR",
426429
"message": "This is an ERROR log, from the parent logger",
427-
"service": "shopping-cart-api",
430+
"service": "serverlessAirline",
428431
"timestamp": "2021-12-12T22:32:54.670Z",
429432
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
430433
}
431434
{
432435
"level": "ERROR",
433436
"message": "This is an ERROR log, from the child logger",
434-
"service": "shopping-cart-api",
437+
"service": "serverlessAirline",
435438
"timestamp": "2021-12-12T22:32:54.670Z",
436439
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
437440
}
@@ -464,7 +467,7 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
464467
sampleRateValue: 0.5
465468
});
466469

467-
const lambdaHandler = async () => {
470+
export const handler = async (_event: any, _context: any) => {
468471

469472
// 0.5 means that you have 50% chance that these logs will be printed
470473
logger.info("This is INFO log #1");
@@ -485,31 +488,31 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
485488
"level": "INFO",
486489
"message": "This is INFO log #1",
487490
"sampling_rate": "0.5",
488-
"service": "shopping-cart-api",
491+
"service": "serverlessAirline",
489492
"timestamp": "2021-12-12T22:59:06.334Z",
490493
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
491494
}
492495
{
493496
"level": "INFO",
494497
"message": "This is INFO log #2",
495498
"sampling_rate": "0.5",
496-
"service": "shopping-cart-api",
499+
"service": "serverlessAirline",
497500
"timestamp": "2021-12-12T22:59:06.337Z",
498501
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
499502
}
500503
{
501504
"level": "INFO",
502505
"message": "This is INFO log #3",
503506
"sampling_rate": "0.5",
504-
"service": "shopping-cart-api",
507+
"service": "serverlessAirline",
505508
"timestamp": "2021-12-12T22:59:06.338Z",
506509
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
507510
}
508511
{
509512
"level": "INFO",
510513
"message": "This is INFO log #4",
511514
"sampling_rate": "0.5",
512-
"service": "shopping-cart-api",
515+
"service": "serverlessAirline",
513516
"timestamp": "2021-12-12T22:59:06.338Z",
514517
"xray_trace_id": "abcdef123456abcdef123456abcdef123456"
515518
}
@@ -528,7 +531,7 @@ You can customize the structure (keys and values) of your log items by passing a
528531
const logger = new Logger({
529532
logFormatter: new MyCompanyLogFormatter(),
530533
logLevel: "DEBUG",
531-
serviceName: "shopping-cart-api",
534+
serviceName: "serverlessAirline",
532535
sampleRateValue: 0.5,
533536
persistentLogAttributes: {
534537
awsAccountId: process.env.AWS_ACCOUNT_ID,
@@ -539,11 +542,13 @@ You can customize the structure (keys and values) of your log items by passing a
539542
},
540543
});
541544

542-
const lambdaHandler: Handler = async (event, context) => {
545+
export const handler = async (event, _context) => {
546+
543547
logger.addContext(context);
544548
545549
logger.info("This is an INFO log", { correlationIds: { myCustomCorrelationId: "foo-bar-baz" } });
546-
};
550+
551+
};
547552
```
548553

549554
This is how the `MyCompanyLogFormatter` (dummy name) would look like:
@@ -598,7 +603,7 @@ This is how the printed log would look:
598603
```json
599604
{
600605
"message": "This is an INFO log",
601-
"service": "shopping-cart-api",
606+
"service": "serverlessAirline",
602607
"awsRegion": "eu-central-1",
603608
"correlationIds": {
604609
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e812345678",
@@ -654,7 +659,7 @@ describe('MyUnitTest', () => {
654659

655660
test('Lambda invoked successfully', async () => {
656661

657-
const testEvent = { test: 'test' };
662+
const testEvent = { test: 'test' };
658663
await handler(testEvent, dummyContext);
659664

660665
});

0 commit comments

Comments
 (0)