-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
53 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
import { Logger } from '@seek/logger'; | ||
import { Context } from 'aws-lambda'; | ||
|
||
import { contextLogger } from 'src/framework/logging'; | ||
import { logger, loggerContext } from 'src/framework/logging'; | ||
|
||
export const createHandler = <Event, Output = unknown>( | ||
fn: (event: Event, ctx: { logger: Logger }) => Promise<Output>, | ||
) => | ||
async function lambdaHandler(event: Event, ctx: Context) { | ||
const logger = contextLogger(ctx); | ||
export const createHandler = | ||
<Event, Output = unknown>(fn: (event: Event) => Promise<Output>) => | ||
(event: Event, { awsRequestId }: Context) => | ||
loggerContext.run({ awsRequestId }, async () => { | ||
try { | ||
const output = await fn(event); | ||
|
||
try { | ||
const output = await fn(event, { logger }); | ||
logger.info('request'); | ||
|
||
logger.info('request'); | ||
return output; | ||
} catch (err) { | ||
logger.error({ err }, 'request'); | ||
|
||
return output; | ||
} catch (err) { | ||
logger.error({ err }, 'request'); | ||
|
||
throw new Error('invoke error'); | ||
} | ||
}; | ||
throw new Error('invoke error'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
|
||
import createLogger from '@seek/logger'; | ||
import { Context } from 'aws-lambda'; | ||
|
||
import { config } from 'src/config'; | ||
|
||
export const rootLogger = createLogger({ | ||
interface LoggerContext { | ||
awsRequestId: string; | ||
} | ||
|
||
export const loggerContext = new AsyncLocalStorage<LoggerContext>(); | ||
|
||
export const logger = createLogger({ | ||
base: { | ||
environment: config.environment, | ||
version: config.version, | ||
}, | ||
|
||
level: config.logLevel, | ||
|
||
mixin: () => loggerContext.getStore() ?? {}, | ||
|
||
name: config.name, | ||
|
||
transport: | ||
config.environment === 'local' ? { target: 'pino-pretty' } : undefined, | ||
}); | ||
|
||
/* istanbul ignore next: @seek/logger interface */ | ||
export const contextLogger = ({ awsRequestId }: Context) => | ||
rootLogger.child({ awsRequestId }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import * as logging from 'src/framework/logging'; | ||
|
||
export const contextLogger = { | ||
export const logger = { | ||
error: jest.fn(), | ||
info: jest.fn(), | ||
|
||
clear: () => { | ||
contextLogger.error.mockClear(); | ||
contextLogger.info.mockClear(); | ||
logger.error.mockClear(); | ||
logger.info.mockClear(); | ||
}, | ||
|
||
spy: () => | ||
jest.spyOn(logging, 'contextLogger').mockReturnValue(contextLogger as any), | ||
spy: () => { | ||
jest.spyOn(logging.logger, 'error').mockImplementation(logger.error); | ||
jest.spyOn(logging.logger, 'info').mockImplementation(logger.info); | ||
}, | ||
}; |