Skip to content

Commit 7222c9a

Browse files
committed
refactor(logger): addressed feedback from team meeting
1 parent 5f867ea commit 7222c9a

21 files changed

+300
-140
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
'no-console': 0,
4949
'no-multi-spaces': [ 'error', { 'ignoreEOLComments': false } ],
5050
'no-multiple-empty-lines': [ 'error', { 'max': 1, 'maxBOF': 0 } ],
51+
'no-throw-literal': 'error',
5152
'object-curly-spacing': [ 'error', 'always' ],
5253
'prefer-arrow-callback': 'error',
5354
'quotes': [ 'error', 'single', { 'allowTemplateLiterals': true } ],

packages/logger/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ logger.error('This is an ERROR log');
5454
// Environment variables set for the Lambda
5555
process.env.LOG_LEVEL = 'WARN';
5656
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
57-
process.env.POWERTOOLS_CONTEXT_ENABLED = 'TRUE';
5857

5958
const logger = new Logger();
6059

@@ -286,9 +285,7 @@ const lambdaHandler: Handler = async () => {
286285
## Custom logger options: log level, service name, sample rate value, log attributes, variables source, log format
287286
288287
```typescript
289-
290288
process.env.CUSTOM_ENV = 'prod';
291-
process.env.POWERTOOLS_CONTEXT_ENABLED = 'TRUE';
292289

293290
// Custom configuration service for variables, and custom formatter to comply to different log JSON schema
294291
import { CustomConfigService } from './config/CustomConfigService';

packages/logger/examples/custom-logger-options.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { populateEnvironmentVariables } from '../tests/helpers';
44
populateEnvironmentVariables();
55
// Additional runtime variables
66
process.env.CUSTOM_ENV = 'prod';
7-
process.env.POWERTOOLS_CONTEXT_ENABLED = 'TRUE';
87

98
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
109
import * as powertool from '../../../package.json';
@@ -18,7 +17,7 @@ const logger = new Logger({
1817
logLevel: 'INFO', // Override options
1918
serviceName: 'foo-bar',
2019
sampleRateValue: 0.00001,
21-
customAttributes: { // Custom attributes that will be added in every log
20+
persistentLogAttributes: { // Custom attributes that will be added in every log
2221
awsAccountId: '123456789012',
2322
logger: {
2423
name: powertool.name,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { populateEnvironmentVariables } from '../tests/helpers';
2+
3+
// Populate runtime
4+
populateEnvironmentVariables();
5+
// Additional runtime variables
6+
process.env.LOG_LEVEL = 'WARN';
7+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
8+
9+
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
10+
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
11+
import { Handler } from 'aws-lambda';
12+
import { Logger } from '../src';
13+
14+
const logger = new Logger();
15+
16+
const lambdaHandler: Handler = async () => {
17+
18+
// Example of use case:
19+
// Log an error with a message (informative string) and
20+
// some extra ephemeral custom attributes to help debugging (like a correlation ID)
21+
22+
// Logs below will result in the same JSON output
23+
24+
const error = new Error('Something bad happened!');
25+
26+
// You can add ephemeral extra custom attributes like this:
27+
logger.error('This is an ERROR log', error, { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
28+
29+
// Or you can also add them like this (same log output):
30+
logger.error({ message: 'This is an ERROR log', correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } }, error);
31+
32+
return {
33+
foo: 'bar'
34+
};
35+
36+
};
37+
38+
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

packages/logger/examples/errors.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { populateEnvironmentVariables } from '../tests/helpers';
2+
3+
// Populate runtime
4+
populateEnvironmentVariables();
5+
// Additional runtime variables
6+
process.env.LOG_LEVEL = 'WARN';
7+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
8+
9+
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
10+
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
11+
import { Handler } from 'aws-lambda';
12+
import { Logger } from '../src';
13+
14+
const logger = new Logger();
15+
16+
const lambdaHandler: Handler = async () => {
17+
18+
try {
19+
throw new Error('Unexpected error #1');
20+
} catch (error) {
21+
logger.error('This is an ERROR log', error);
22+
}
23+
24+
try {
25+
throw new Error('Unexpected error #2');
26+
} catch (error) {
27+
logger.error('This is an ERROR log', { myCustomErrorKey: error } );
28+
}
29+
30+
return {
31+
foo: 'bar'
32+
};
33+
34+
};
35+
36+
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

packages/logger/examples/formatters/CustomLogFormatter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type MyCompanyLog = LogAttributes;
55

66
class CustomLogFormatter extends LogFormatter {
77

8-
public format(attributes: UnformattedAttributes): MyCompanyLog {
8+
public formatAttributes(attributes: UnformattedAttributes): MyCompanyLog {
99
return {
1010
message: attributes.message,
1111
service: attributes.serviceName,
@@ -16,10 +16,10 @@ class CustomLogFormatter extends LogFormatter {
1616
xRayTraceId: attributes.xRayTraceId
1717
},
1818
lambdaFunction: {
19-
name: attributes.lambdaContext?.name,
20-
arn: attributes.lambdaContext?.arn,
19+
name: attributes.lambdaContext?.functionName,
20+
arn: attributes.lambdaContext?.invokedFunctionArn,
2121
memoryLimitInMB: attributes.lambdaContext?.memoryLimitInMB,
22-
version: attributes.lambdaContext?.version,
22+
version: attributes.lambdaContext?.functionVersion,
2323
coldStart: attributes.lambdaContext?.coldStart,
2424
},
2525
logLevel: attributes.logLevel,

packages/logger/examples/hello-world-with-context.ts renamed to packages/logger/examples/inject-context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ populateEnvironmentVariables();
55
// Additional runtime variables
66
process.env.LOG_LEVEL = 'WARN';
77
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
8-
process.env.POWERTOOLS_CONTEXT_ENABLED = 'TRUE';
98

109
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
1110
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { populateEnvironmentVariables } from '../tests/helpers';
2+
3+
// Populate runtime
4+
populateEnvironmentVariables();
5+
// Additional runtime variables
6+
process.env.LOG_LEVEL = 'INFO';
7+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
8+
9+
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
10+
import * as powertool from '../../../package.json';
11+
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
12+
import { Handler } from 'aws-lambda';
13+
import { Logger } from '../src';
14+
15+
// You can add persistent extra custom attributes directly in the constructor of the logger:
16+
const logger = new Logger();
17+
18+
const lambdaHandler: Handler = async () => {
19+
20+
// Or you can choose to add persistent log keys to an existing Logger instance:
21+
logger.appendKeys({
22+
aws_account_id: '123456789012',
23+
aws_region: 'eu-central-1',
24+
logger: {
25+
name: powertool.name,
26+
version: powertool.version,
27+
}
28+
});
29+
30+
// This info log will print all extra custom attributes added above
31+
// Extra attributes: logger object with name and version of this library, awsAccountId, awsRegion
32+
logger.info('This is an INFO log');
33+
34+
return {
35+
foo: 'bar'
36+
};
37+
38+
};
39+
40+
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

packages/logger/examples/sample-rate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const lambdaHandler: Handler = async () => {
2121
logger.info('This is INFO log #3');
2222
logger.info('This is INFO log #4');
2323

24+
// Refresh sample rate calculation on runtime
25+
logger.refreshSampleRateCalculation();
26+
2427
return {
2528
foo: 'bar'
2629
};

packages/logger/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
"version": "npm run format && git add -A src",
2020
"postversion": "git push && git push --tags",
2121
"example:hello-world": "ts-node examples/hello-world.ts",
22-
"example:hello-world-with-context": "ts-node examples/hello-world-with-context.ts",
22+
"example:hello-world-with-context": "ts-node examples/inject-context.ts",
23+
"example:hello-world-errors": "ts-node examples/errors.ts",
2324
"example:custom-logger-options": "ts-node examples/custom-logger-options.ts",
2425
"example:child-logger": "ts-node examples/child-logger.ts",
2526
"example:additional-keys": "ts-node examples/additional-keys.ts",
26-
"example:sample-rate": "ts-node examples/sample-rate.ts"
27+
"example:sample-rate": "ts-node examples/sample-rate.ts",
28+
"example:persistent-attributes": "ts-node examples/persistent-attributes.ts",
29+
"example:ephemeral-attributes": "ts-node examples/ephemeral-attributes.ts"
2730
},
2831
"homepage": "https://github.com/awslabs/aws-lambda-powertools-typescript/tree/master/packages/logging#readme",
2932
"license": "MIT",

0 commit comments

Comments
 (0)