From 08f3c92baf6b2e9e04b622379598a14f4647dbe1 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Mon, 31 Aug 2020 13:44:30 -0700 Subject: [PATCH] feat: log metadata/input/output/request/response (#1478) --- .../middleware-logger/src/loggerMiddleware.ts | 42 ++++++++++++++++--- packages/types/src/logger.ts | 8 ++-- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/packages/middleware-logger/src/loggerMiddleware.ts b/packages/middleware-logger/src/loggerMiddleware.ts index 86c31f8b9800..39847d1b96bf 100644 --- a/packages/middleware-logger/src/loggerMiddleware.ts +++ b/packages/middleware-logger/src/loggerMiddleware.ts @@ -4,20 +4,49 @@ import { FinalizeHandlerArguments, FinalizeHandlerOutput, FinalizeRequestHandlerOptions, + HandlerExecutionContext, MetadataBearer, Pluggable, } from "@aws-sdk/types"; import { LoggerResolvedConfig } from "./configurations"; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export const loggerMiddleware = (options: LoggerResolvedConfig) => ( - next: FinalizeHandler +export const loggerMiddleware = () => ( + next: FinalizeHandler, + context: HandlerExecutionContext ): FinalizeHandler => async ( args: FinalizeHandlerArguments ): Promise> => { - // TODO: use and call options.logger once it's available in context - return next(args); + const { logger, inputFilterSensitiveLog, outputFilterSensitiveLog } = context; + + const response = await next(args); + + if (!logger) { + return response; + } + + const { + output: { $metadata, ...outputWithoutMetadata }, + } = response; + + if (typeof logger.debug === "function") { + logger.debug({ + httpRequest: { ...(args.request as any), body: "examine input under info" }, + }); + logger.debug({ + httpResponse: { ...(response.response as any), body: "examine output under info" }, + }); + } + + if (typeof logger.info === "function") { + logger.info({ + $metadata, + input: inputFilterSensitiveLog(args.input), + output: outputFilterSensitiveLog(outputWithoutMetadata), + }); + } + + return response; }; export const loggerMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = { @@ -26,8 +55,9 @@ export const loggerMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLo step: "finalizeRequest", }; +// eslint-disable-next-line @typescript-eslint/no-unused-vars export const getLoggerPlugin = (options: LoggerResolvedConfig): Pluggable => ({ applyToStack: (clientStack) => { - clientStack.add(loggerMiddleware(options), loggerMiddlewareOptions); + clientStack.add(loggerMiddleware(), loggerMiddlewareOptions); }, }); diff --git a/packages/types/src/logger.ts b/packages/types/src/logger.ts index 6aae059f3e70..e8f23cce332b 100644 --- a/packages/types/src/logger.ts +++ b/packages/types/src/logger.ts @@ -21,8 +21,8 @@ export interface LoggerOptions { * throughout the middleware stack. */ export interface Logger { - log(content: string): void; - info(content: string): void; - warn(content: string): void; - error(content: string): void; + debug(content: object): void; + info(content: object): void; + warn(content: object): void; + error(content: object): void; }