Skip to content

Commit

Permalink
feat: log metadata/input/output/request/response (#1478)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Aug 31, 2020
1 parent 88b0fb7 commit 08f3c92
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
42 changes: 36 additions & 6 deletions packages/middleware-logger/src/loggerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => <Output extends MetadataBearer = MetadataBearer>(
next: FinalizeHandler<any, Output>
export const loggerMiddleware = () => <Output extends MetadataBearer = MetadataBearer>(
next: FinalizeHandler<any, Output>,
context: HandlerExecutionContext
): FinalizeHandler<any, Output> => async (
args: FinalizeHandlerArguments<any>
): Promise<FinalizeHandlerOutput<Output>> => {
// 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 = {
Expand All @@ -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<any, any> => ({
applyToStack: (clientStack) => {
clientStack.add(loggerMiddleware(options), loggerMiddlewareOptions);
clientStack.add(loggerMiddleware(), loggerMiddlewareOptions);
},
});
8 changes: 4 additions & 4 deletions packages/types/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 08f3c92

Please sign in to comment.