Skip to content

Commit 08f3c92

Browse files
authored
feat: log metadata/input/output/request/response (#1478)
1 parent 88b0fb7 commit 08f3c92

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

Diff for: packages/middleware-logger/src/loggerMiddleware.ts

+36-6
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,49 @@ import {
44
FinalizeHandlerArguments,
55
FinalizeHandlerOutput,
66
FinalizeRequestHandlerOptions,
7+
HandlerExecutionContext,
78
MetadataBearer,
89
Pluggable,
910
} from "@aws-sdk/types";
1011

1112
import { LoggerResolvedConfig } from "./configurations";
1213

13-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14-
export const loggerMiddleware = (options: LoggerResolvedConfig) => <Output extends MetadataBearer = MetadataBearer>(
15-
next: FinalizeHandler<any, Output>
14+
export const loggerMiddleware = () => <Output extends MetadataBearer = MetadataBearer>(
15+
next: FinalizeHandler<any, Output>,
16+
context: HandlerExecutionContext
1617
): FinalizeHandler<any, Output> => async (
1718
args: FinalizeHandlerArguments<any>
1819
): Promise<FinalizeHandlerOutput<Output>> => {
19-
// TODO: use and call options.logger once it's available in context
20-
return next(args);
20+
const { logger, inputFilterSensitiveLog, outputFilterSensitiveLog } = context;
21+
22+
const response = await next(args);
23+
24+
if (!logger) {
25+
return response;
26+
}
27+
28+
const {
29+
output: { $metadata, ...outputWithoutMetadata },
30+
} = response;
31+
32+
if (typeof logger.debug === "function") {
33+
logger.debug({
34+
httpRequest: { ...(args.request as any), body: "examine input under info" },
35+
});
36+
logger.debug({
37+
httpResponse: { ...(response.response as any), body: "examine output under info" },
38+
});
39+
}
40+
41+
if (typeof logger.info === "function") {
42+
logger.info({
43+
$metadata,
44+
input: inputFilterSensitiveLog(args.input),
45+
output: outputFilterSensitiveLog(outputWithoutMetadata),
46+
});
47+
}
48+
49+
return response;
2150
};
2251

2352
export const loggerMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation = {
@@ -26,8 +55,9 @@ export const loggerMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLo
2655
step: "finalizeRequest",
2756
};
2857

58+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2959
export const getLoggerPlugin = (options: LoggerResolvedConfig): Pluggable<any, any> => ({
3060
applyToStack: (clientStack) => {
31-
clientStack.add(loggerMiddleware(options), loggerMiddlewareOptions);
61+
clientStack.add(loggerMiddleware(), loggerMiddlewareOptions);
3262
},
3363
});

Diff for: packages/types/src/logger.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export interface LoggerOptions {
2121
* throughout the middleware stack.
2222
*/
2323
export interface Logger {
24-
log(content: string): void;
25-
info(content: string): void;
26-
warn(content: string): void;
27-
error(content: string): void;
24+
debug(content: object): void;
25+
info(content: object): void;
26+
warn(content: object): void;
27+
error(content: object): void;
2828
}

0 commit comments

Comments
 (0)