Skip to content

Commit 369e4d1

Browse files
authored
feat(logger): add context decorator functionality (aws-powertools#13)
* refactor(logger): Logger class * test(logger): set coverage 100 * test(logger): disabled coverage check temporarily * feat(logger): add context decorator functionality * docs(logger): Lambda first letter uppercase * fix(logger): remove duplicate import line * chore(logger): Lambda first letter uppercase * test(all): hide implementation detail of tests in package
1 parent 11fb85a commit 369e4d1

15 files changed

+353
-136
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"commit": "commit",
99
"lerna-ci": "lerna exec -- npm ci",
10-
"lerna-test": "lerna exec -- jest --coverage --detectOpenHandles",
10+
"lerna-test": "lerna exec -- npm run test",
1111
"lerna-build": "lerna exec -- tsc",
1212
"lerna-lint": "lerna exec -- eslint \"./{src,tests}/**/*.ts\"",
1313
"lerna-format": "lerna exec -- eslint --fix \"./{src,tests}/**/*.ts\"",

packages/logger/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ logger.error('This is an ERROR log');
5050

5151
### Capturing Lambda context info
5252

53+
Without decorators:
54+
5355
```typescript
5456
// Environment variables set for the Lambda
5557
process.env.LOG_LEVEL = 'WARN';
@@ -108,6 +110,51 @@ const lambdaHandler: Handler = async (event, context) => {
108110
</details>
109111

110112

113+
With decorators:
114+
115+
```typescript
116+
// Environment variables set for the Lambda
117+
process.env.LOG_LEVEL = 'INFO';
118+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
119+
120+
const logger = new Logger();
121+
122+
class Lambda implements LambdaInterface {
123+
124+
@logger.injectLambdaContext()
125+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
126+
127+
logger.info('This is an INFO log with some context');
128+
129+
}
130+
131+
}
132+
133+
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
134+
135+
```
136+
137+
<details>
138+
<summary>Click to expand and see the logs outputs</summary>
139+
140+
```bash
141+
142+
{
143+
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
144+
lambda_function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:Example',
145+
lambda_function_memory_size: 128,
146+
lambda_function_name: 'foo-bar-function',
147+
level: 'INFO',
148+
message: 'This is an INFO log with some context',
149+
service: 'hello-world',
150+
timestamp: '2021-03-17T08:25:41.198Z',
151+
xray_trace_id: 'abcdef123456abcdef123456abcdef123456'
152+
}
153+
154+
```
155+
</details>
156+
157+
111158
### Appending additional keys
112159

113160
```typescript

packages/logger/examples/child-logger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ const lambdaHandler: Handler = async () => {
3030

3131
};
3232

33-
lambdaHandler(dummyEvent, dummyContext, () => {});
33+
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
11+
import { LambdaInterface } from '../src/lambda/LambdaInterface';
12+
import { Logger } from '../src';
13+
import { Callback, Context } from 'aws-lambda/handler';
14+
15+
const logger = new Logger();
16+
17+
class Lambda implements LambdaInterface {
18+
19+
@logger.injectLambdaContext()
20+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
21+
22+
logger.info('This is an INFO log with some context');
23+
24+
}
25+
26+
}
27+
28+
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

packages/logger/examples/sample-rate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ const lambdaHandler: Handler = async () => {
2727

2828
};
2929

30-
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
30+
lambdaHandler(dummyEvent, dummyContext, () => console.log('lambda invoked!'));

packages/logger/jest.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ module.exports = {
2222
],
2323
'coverageThreshold': {
2424
'global': {
25-
'statements': 70,
26-
'branches': 60,
27-
'functions': 70,
28-
'lines': 70,
25+
'statements': 100,
26+
'branches': 100,
27+
'functions': 100,
28+
'lines': 100,
2929
},
3030
},
3131
'coverageReporters': [

packages/logger/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"scripts": {
1010
"commit": "commit",
11-
"test": "jest --coverage --detectOpenHandles",
11+
"test": "jest --detectOpenHandles",
1212
"watch": "jest --watch",
1313
"build": "tsc",
1414
"lint": "eslint \"./{src,tests}/**/*.ts\"",
@@ -20,6 +20,7 @@
2020
"postversion": "git push && git push --tags",
2121
"example:hello-world": "ts-node examples/hello-world.ts",
2222
"example:hello-world-with-context": "ts-node examples/hello-world-with-context.ts",
23+
"example:hello-world-with-context-decorators": "ts-node examples/hello-world-with-context-decorators.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",

0 commit comments

Comments
 (0)