forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWire.java
More file actions
146 lines (127 loc) · 5 KB
/
Wire.java
File metadata and controls
146 lines (127 loc) · 5 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed 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
*
* http://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 feign;
import com.google.common.io.Closer;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map.Entry;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
/* Writes http headers and body. Plumb to your favorite log impl. */
public abstract class Wire {
/* logs to the category {@link Wire} at {@link Level#FINE}. */
public static class ErrorWire extends Wire {
final Logger logger = Logger.getLogger(Wire.class.getName());
@Override protected void log(Target<?> target, String format, Object... args) {
System.err.printf(format + "%n", args);
}
}
/* logs to the category {@link Wire} at {@link Level#FINE}, if loggable. */
public static class LoggingWire extends Wire {
final Logger logger = Logger.getLogger(Wire.class.getName());
@Override void wireRequest(Target<?> target, Request request) {
if (logger.isLoggable(Level.FINE)) {
super.wireRequest(target, request);
}
}
@Override Response wireAndRebufferResponse(Target<?> target, Response response) throws IOException {
if (logger.isLoggable(Level.FINE)) {
return super.wireAndRebufferResponse(target, response);
}
return response;
}
@Override protected void log(Target<?> target, String format, Object... args) {
logger.fine(String.format(format, args));
}
/* helper that configures jul to sanely log messages. */
public LoggingWire appendToFile(String logfile) {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
logger.setLevel(Level.FINE);
try {
FileHandler handler = new FileHandler(logfile, true);
handler.setFormatter(new SimpleFormatter() {
@Override
public String format(LogRecord record) {
String timestamp = sdf.format(new java.util.Date(record.getMillis())); // NOPMD
return String.format("%s %s%n", timestamp, record.getMessage()); // NOPMD
}
});
logger.addHandler(handler);
} catch (IOException e) {
}
return this;
}
}
public static class NoOpWire extends Wire {
@Override void wireRequest(Target<?> target, Request request) {
}
@Override Response wireAndRebufferResponse(Target<?> target, Response response) throws IOException {
return response;
}
@Override
protected void log(Target<?> target, String format, Object... args) {
}
}
/**
* Override to log requests and responses using your own implementation.
* Messages will be http request and response text.
*
* @param target useful if using MDC (Mapped Diagnostic Context) loggers
* @param format {@link java.util.Formatter format string}
* @param args arguments applied to {@code format}
*/
protected abstract void log(Target<?> target, String format, Object... args);
void wireRequest(Target<?> target, Request request) {
log(target, ">> %s %s HTTP/1.1", request.method(), request.url());
for (Entry<String, String> header : request.headers().entries()) {
log(target, ">> %s: %s", header.getKey(), header.getValue());
}
if (request.body().isPresent()) {
log(target, ">> "); // CRLF
log(target, ">> %s", request.body().get());
}
}
Response wireAndRebufferResponse(Target<?> target, Response response) throws IOException {
log(target, "<< HTTP/1.1 %s %s", response.status(), response.reason());
for (Entry<String, String> header : response.headers().entries()) {
log(target, "<< %s: %s", header.getKey(), header.getValue());
}
if (response.body().isPresent()) {
log(target, "<< "); // CRLF
Closer closer = Closer.create();
try {
StringBuilder body = new StringBuilder();
BufferedReader reader = new BufferedReader(closer.register(response.body().get().asReader()));
String line;
while ((line = reader.readLine()) != null) {
body.append(line);
log(target, "<< %s", line);
}
return Response.create(response.status(), response.reason(), response.headers(), body.toString());
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
return response;
}
}