forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingClient.java
89 lines (79 loc) · 3.83 KB
/
LoggingClient.java
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
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.client.logging;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.HttpClient;
import com.linecorp.armeria.common.HttpHeaders;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.RequestContext;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.logging.LogLevel;
import com.linecorp.armeria.common.logging.RequestLog;
import com.linecorp.armeria.common.logging.RequestOnlyLog;
import com.linecorp.armeria.common.util.Sampler;
/**
* Decorates an {@link HttpClient} to log {@link Request}s and {@link Response}s.
*/
public final class LoggingClient extends AbstractLoggingClient<HttpRequest, HttpResponse>
implements HttpClient {
/**
* Returns a new {@link HttpClient} decorator that logs {@link Request}s and {@link Response}s at
* {@link LogLevel#INFO} for success, {@link LogLevel#WARN} for failure.
*
* @see LoggingClientBuilder for more information on the default settings.
*/
public static Function<? super HttpClient, LoggingClient> newDecorator() {
return builder().requestLogLevel(LogLevel.INFO)
.successfulResponseLogLevel(LogLevel.INFO)
.failureResponseLogLevel(LogLevel.WARN)
.newDecorator();
}
/**
* Returns a newly created {@link LoggingClientBuilder}.
*/
public static LoggingClientBuilder builder() {
return new LoggingClientBuilder();
}
/**
* Creates a new instance that logs {@link Request}s and {@link Response}s at the specified
* {@link LogLevel}s with the specified sanitizers.
* If the logger is null, it means that the default logger is used.
*/
LoggingClient(
HttpClient delegate,
@Nullable Logger logger,
Function<? super RequestOnlyLog, LogLevel> requestLogLevelMapper,
Function<? super RequestLog, LogLevel> responseLogLevelMapper,
BiFunction<? super RequestContext, ? super HttpHeaders, ?> requestHeadersSanitizer,
BiFunction<? super RequestContext, Object, ?> requestContentSanitizer,
BiFunction<? super RequestContext, ? super HttpHeaders, ?> requestTrailersSanitizer,
BiFunction<? super RequestContext, ? super HttpHeaders, ?> responseHeadersSanitizer,
BiFunction<? super RequestContext, Object, ?> responseContentSanitizer,
BiFunction<? super RequestContext, ? super HttpHeaders, ?> responseTrailersSanitizer,
BiFunction<? super RequestContext, ? super Throwable, ?> responseCauseSanitizer,
Sampler<? super ClientRequestContext> sampler) {
super(delegate, logger, requestLogLevelMapper, responseLogLevelMapper,
requestHeadersSanitizer, requestContentSanitizer, requestTrailersSanitizer,
responseHeadersSanitizer, responseContentSanitizer, responseTrailersSanitizer,
responseCauseSanitizer, sampler);
}
}