Skip to content

Commit

Permalink
Add changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c committed May 24, 2022
1 parent fa8125c commit 65d0353
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
10 changes: 9 additions & 1 deletion .changeset/olive-worms-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ Before:

```typescript
import createLogger from '@seek/logger';
import Koa from 'koa';
import Koa, { Context } from 'koa';
import { RequestLogging } from 'seek-koala';

const rootLogger = createLogger();

const contextLogger = (ctx: Context) =>
rootLogger.child(RequestLogging.contextFields(ctx));

const app = new Koa().use((ctx) => {
rootLogger.info('Has no context');

contextLogger(ctx).info('Has context');
});
```
Expand All @@ -27,10 +31,14 @@ After:
import createLogger from '@seek/logger';
import Koa from 'koa';
import { RequestLogging } from 'seek-koala';

const { createContextMiddleware, mixin } =
RequestLogging.createContextStorage();

const contextMiddleware = createContextMiddleware();

const logger = createLogger({ mixin });

const app = new Koa().use(contextMiddleware).use((ctx) => {
logger.info('Has context');
});
Expand Down
45 changes: 45 additions & 0 deletions .changeset/pink-flowers-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
'skuba': patch
---

template/lambda-sqs-worker: Use [AsyncLocalStorage](https://nodejs.org/docs/latest-v16.x/api/async_context.html#asynchronous-context-tracking) to track logger context

We now employ this Node.js API to thread logging context through the handler of your Lambda function. This enables use of a singleton `logger` instance instead of manually propagating Lambda context and juggling `rootLogger`s and `contextLogger`s, and is equivalent to #864.

Before:

```typescript
import createLogger from '@seek/logger';
import { Context } from 'aws-lambda';

const rootLogger = createLogger();

const contextLogger = ({ awsRequestId }: Context) =>
rootLogger.child({ awsRequestId });

const handler = async (_event: unknown, ctx: Context) => {
rootLogger.info('Has no context');

contextLogger(ctx).info('Has context');
};
```

After:

```typescript
import { AsyncLocalStorage } from 'async_hooks';

import createLogger from '@seek/logger';
import { Context } from 'aws-lambda';

const loggerContext = new AsyncLocalStorage<{ awsRequestId: string }>();

const logger = createLogger({
mixin: () => loggerContext.getStore() ?? {},
});

const handler = (_event: unknown, { awsRequestId }: Context) =>
loggerContext.run({ awsRequestId }, async () => {
logger.info('Has context');
});
```

0 comments on commit 65d0353

Please sign in to comment.