/
logPolicy.ts
119 lines (104 loc) · 3.97 KB
/
logPolicy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { HttpOperationResponse } from "../httpOperationResponse";
import { WebResourceLike } from "../webResource";
import {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions
} from "./requestPolicy";
import { Debugger } from "@azure/logger";
import { logger as coreLogger } from "../log";
import { Sanitizer } from "../util/sanitizer";
export interface LogPolicyOptions {
/**
* Header names whose values will be logged when logging is enabled. Defaults to
* Date, traceparent, x-ms-client-request-id, and x-ms-request id. Any headers
* specified in this field will be added to that list. Any other values will
* be written to logs as "REDACTED".
*/
allowedHeaderNames?: string[];
/**
* Query string names whose values will be logged when logging is enabled. By default no
* query string values are logged.
*/
allowedQueryParameters?: string[];
/**
* The Debugger (logger) instance to use for writing pipeline logs.
*/
logger?: Debugger;
}
export function logPolicy(loggingOptions: LogPolicyOptions = {}): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new LogPolicy(nextPolicy, options, loggingOptions);
}
};
}
export class LogPolicy extends BaseRequestPolicy {
logger: Debugger;
sanitizer: Sanitizer;
/**
* Header names whose values will be logged when logging is enabled. Defaults to
* Date, traceparent, x-ms-client-request-id, and x-ms-request id. Any headers
* specified in this field will be added to that list. Any other values will
* be written to logs as "REDACTED".
* @deprecated Pass these into the constructor instead.
*/
public get allowedHeaderNames(): Set<string> {
return this.sanitizer.allowedHeaderNames;
}
/**
* Header names whose values will be logged when logging is enabled. Defaults to
* Date, traceparent, x-ms-client-request-id, and x-ms-request id. Any headers
* specified in this field will be added to that list. Any other values will
* be written to logs as "REDACTED".
* @deprecated Pass these into the constructor instead.
*/
public set allowedHeaderNames(allowedHeaderNames: Set<string>) {
this.sanitizer.allowedHeaderNames = allowedHeaderNames;
}
/**
* Query string names whose values will be logged when logging is enabled. By default no
* query string values are logged.
* @deprecated Pass these into the constructor instead.
*/
public get allowedQueryParameters(): Set<string> {
return this.sanitizer.allowedQueryParameters;
}
/**
* Query string names whose values will be logged when logging is enabled. By default no
* query string values are logged.
* @deprecated Pass these into the constructor instead.
*/
public set allowedQueryParameters(allowedQueryParameters: Set<string>) {
this.sanitizer.allowedQueryParameters = allowedQueryParameters;
}
constructor(
nextPolicy: RequestPolicy,
options: RequestPolicyOptions,
{
logger = coreLogger.info,
allowedHeaderNames = [],
allowedQueryParameters = []
}: LogPolicyOptions = {}
) {
super(nextPolicy, options);
this.logger = logger;
this.sanitizer = new Sanitizer({ allowedHeaderNames, allowedQueryParameters });
}
public sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
if (!this.logger.enabled) return this._nextPolicy.sendRequest(request);
this.logRequest(request);
return this._nextPolicy.sendRequest(request).then((response) => this.logResponse(response));
}
private logRequest(request: WebResourceLike): void {
this.logger(`Request: ${this.sanitizer.sanitize(request)}`);
}
private logResponse(response: HttpOperationResponse): HttpOperationResponse {
this.logger(`Response status code: ${response.status}`);
this.logger(`Headers: ${this.sanitizer.sanitize(response.headers)}`);
return response;
}
}