Skip to content

feat(logger): add context decorator functionality #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"commit": "commit",
"lerna-ci": "lerna exec -- npm ci",
"lerna-test": "lerna exec -- jest --coverage --detectOpenHandles",
"lerna-test": "lerna exec -- npm run test",
"lerna-build": "lerna exec -- tsc",
"lerna-lint": "lerna exec -- eslint \"./{src,tests}/**/*.ts\"",
"lerna-format": "lerna exec -- eslint --fix \"./{src,tests}/**/*.ts\"",
Expand Down
47 changes: 47 additions & 0 deletions packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ logger.error('This is an ERROR log');

### Capturing Lambda context info

Without decorators:

```typescript
// Environment variables set for the Lambda
process.env.LOG_LEVEL = 'WARN';
Expand Down Expand Up @@ -108,6 +110,51 @@ const lambdaHandler: Handler = async (event, context) => {
</details>


With decorators:

```typescript
// Environment variables set for the Lambda
process.env.LOG_LEVEL = 'INFO';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';

const logger = new Logger();

class Lambda implements LambdaInterface {

@logger.injectLambdaContext()
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {

logger.info('This is an INFO log with some context');

}

}

new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

```

<details>
<summary>Click to expand and see the logs outputs</summary>

```bash

{
aws_request_id: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
lambda_function_arn: 'arn:aws:lambda:eu-central-1:123456789012:function:Example',
lambda_function_memory_size: 128,
lambda_function_name: 'foo-bar-function',
level: 'INFO',
message: 'This is an INFO log with some context',
service: 'hello-world',
timestamp: '2021-03-17T08:25:41.198Z',
xray_trace_id: 'abcdef123456abcdef123456abcdef123456'
}

```
</details>


### Appending additional keys

```typescript
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/examples/child-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ const lambdaHandler: Handler = async () => {

};

lambdaHandler(dummyEvent, dummyContext, () => {});
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
28 changes: 28 additions & 0 deletions packages/logger/examples/hello-world-with-context-decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { populateEnvironmentVariables } from '../tests/helpers';

// Populate runtime
populateEnvironmentVariables();
// Additional runtime variables
process.env.LOG_LEVEL = 'INFO';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../src/lambda/LambdaInterface';
import { Logger } from '../src';
import { Callback, Context } from 'aws-lambda/handler';

const logger = new Logger();

class Lambda implements LambdaInterface {

@logger.injectLambdaContext()
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {

logger.info('This is an INFO log with some context');

}

}

new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
2 changes: 1 addition & 1 deletion packages/logger/examples/sample-rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ const lambdaHandler: Handler = async () => {

};

lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
lambdaHandler(dummyEvent, dummyContext, () => console.log('lambda invoked!'));
8 changes: 4 additions & 4 deletions packages/logger/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ module.exports = {
],
'coverageThreshold': {
'global': {
'statements': 70,
'branches': 60,
'functions': 70,
'lines': 70,
'statements': 100,
'branches': 100,
'functions': 100,
'lines': 100,
},
},
'coverageReporters': [
Expand Down
3 changes: 2 additions & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"commit": "commit",
"test": "jest --coverage --detectOpenHandles",
"test": "jest --detectOpenHandles",
"watch": "jest --watch",
"build": "tsc",
"lint": "eslint \"./{src,tests}/**/*.ts\"",
Expand All @@ -20,6 +20,7 @@
"postversion": "git push && git push --tags",
"example:hello-world": "ts-node examples/hello-world.ts",
"example:hello-world-with-context": "ts-node examples/hello-world-with-context.ts",
"example:hello-world-with-context-decorators": "ts-node examples/hello-world-with-context-decorators.ts",
"example:custom-logger-options": "ts-node examples/custom-logger-options.ts",
"example:child-logger": "ts-node examples/child-logger.ts",
"example:additional-keys": "ts-node examples/additional-keys.ts",
Expand Down
Loading