-
Notifications
You must be signed in to change notification settings - Fork 630
/
ReactorNettyLoggingHandler.java
314 lines (293 loc) · 9.81 KB
/
ReactorNettyLoggingHandler.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2020-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* 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 reactor.netty5.transport.logging;
import java.nio.charset.Charset;
import java.util.Objects;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty5.buffer.BufferUtil;
import io.netty5.buffer.Buffer;
import io.netty5.channel.Channel;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.handler.logging.BufferFormat;
import io.netty5.handler.logging.LogLevel;
import io.netty5.handler.logging.LoggingHandler;
import reactor.netty5.ChannelOperationsId;
import reactor.netty5.Connection;
import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty5.util.internal.StringUtil.NEWLINE;
import static java.util.Objects.requireNonNull;
import static reactor.netty5.transport.logging.AdvancedBufferFormat.HEX_DUMP;
import static reactor.netty5.transport.logging.AdvancedBufferFormat.SIMPLE;
import static reactor.netty5.transport.logging.AdvancedBufferFormat.TEXTUAL;
/**
* Extends {@link LoggingHandler} in order to provide extended connection id, which in case of HTTP
* adds the serial number of the request received on that connection.
* <p>
* In case of {@link AdvancedBufferFormat#TEXTUAL} logs all events in a
* textual representation, so it's human readable and less verbose.
* <p>
* Hint: Logger escapes newlines as "\n" to reduce output.
*
* @author Maximilian Goeke
* @author Violeta Georgieva
* @since 1.0.0
*/
final class ReactorNettyLoggingHandler extends LoggingHandler {
private final AdvancedBufferFormat bufferFormat;
private final Charset charset;
private final String name;
/**
* Creates a new instance with the specified logger name, level and byte buffer format.
*
* @param name the name of the class to use for the logger
* @param level the log level
* @param bufferFormat the byte buffer format
*/
ReactorNettyLoggingHandler(String name, LogLevel level, AdvancedBufferFormat bufferFormat) {
super(name, level, bufferFormat == SIMPLE ? BufferFormat.SIMPLE : BufferFormat.HEX_DUMP);
this.bufferFormat = bufferFormat;
this.charset = null;
this.name = name;
}
/**
* Creates a new instance with the specified logger name, level and charset.
* The byte buffer format is {@link AdvancedBufferFormat#TEXTUAL}
*
* @param name the name of the class to use for the logger
* @param level the log level
* @param charset the charset used to decode the ByteBuf
*/
ReactorNettyLoggingHandler(final String name, final LogLevel level, final Charset charset) {
super(name, level);
this.bufferFormat = TEXTUAL;
this.charset = requireNonNull(charset, "charset");
this.name = name;
}
/*
* The UnsupportedOperationException is thrown to reduce confusion. ReactorNettyLoggingHandler is using
* the AdvancedBufferFormat and not BufferFormat.
*/
@Override
public BufferFormat bufferFormat() {
if (bufferFormat == SIMPLE) {
return BufferFormat.SIMPLE;
}
else if (bufferFormat == HEX_DUMP) {
return BufferFormat.HEX_DUMP;
}
throw new UnsupportedOperationException("ReactorNettyLoggingHandler isn't using the classic BufferFormat.");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ReactorNettyLoggingHandler that)) {
return false;
}
return bufferFormat == that.bufferFormat &&
Objects.equals(charset, that.charset) &&
level() == that.level() &&
name.equals(that.name);
}
@Override
public int hashCode() {
return Objects.hash(bufferFormat, charset, level(), name);
}
@Override
protected String format(ChannelHandlerContext ctx, String eventName) {
String chStr = channelString(ctx.channel());
return new StringBuilder(chStr.length() + 1 + eventName.length())
.append(chStr)
.append(' ')
.append(eventName)
.toString();
}
@Override
protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
if (arg instanceof Buffer buffer) {
return formatBuffer(ctx, eventName, buffer);
}
else if (arg instanceof ByteBufHolder byteBufHolder) {
return formatByteBufHolder(ctx, eventName, byteBufHolder);
}
else {
return formatSimple(ctx, eventName, arg);
}
}
@Override
protected String format(ChannelHandlerContext ctx, String eventName, Object firstArg, Object secondArg) {
if (secondArg == null) {
return formatSimple(ctx, eventName, firstArg);
}
String chStr = channelString(ctx.channel());
String arg1Str = String.valueOf(firstArg);
String arg2Str = secondArg.toString();
return new StringBuilder(chStr.length() + 1 + eventName.length() + 2 + arg1Str.length() + 2 + arg2Str.length())
.append(chStr)
.append(' ')
.append(eventName)
.append(": ")
.append(arg1Str)
.append(", ")
.append(arg2Str)
.toString();
}
private String channelString(Channel channel) {
String channelStr;
StringBuilder result;
Connection connection = Connection.from(channel);
if (connection instanceof ChannelOperationsId channelOperationsId) {
channelStr = channelOperationsId.asLongText();
if (channelStr.charAt(0) != TRACE_ID_PREFIX) {
result = new StringBuilder(1 + channelStr.length() + 1)
.append(CHANNEL_ID_PREFIX)
.append(channelStr)
.append(CHANNEL_ID_SUFFIX);
}
else {
result = new StringBuilder(channelStr);
}
}
else {
channelStr = channel.toString();
if (channelStr.charAt(0) == CHANNEL_ID_PREFIX) {
channelStr = channelStr.substring(ORIGINAL_CHANNEL_ID_PREFIX_LENGTH);
result = new StringBuilder(1 + channelStr.length())
.append(CHANNEL_ID_PREFIX)
.append(channelStr);
}
else {
int ind = channelStr.indexOf(ORIGINAL_CHANNEL_ID_PREFIX);
result = new StringBuilder(1 + (channelStr.length() - ORIGINAL_CHANNEL_ID_PREFIX_LENGTH))
.append(channelStr.substring(0, ind))
.append(CHANNEL_ID_PREFIX)
.append(channelStr.substring(ind + ORIGINAL_CHANNEL_ID_PREFIX_LENGTH));
}
}
return result.toString();
}
private String formatBuffer(ChannelHandlerContext ctx, String eventName, Buffer msg) {
String chStr = channelString(ctx.channel());
int length = msg.readableBytes();
if (length == 0) {
return new StringBuilder(chStr.length() + 1 + eventName.length() + 4)
.append(chStr)
.append(' ')
.append(eventName)
.append(": 0B")
.toString();
}
else {
int outputLength = chStr.length() + 1 + eventName.length() + 2 + 10 + 1;
String message = "";
if (bufferFormat == HEX_DUMP) {
int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
int hexDumpLength = 2 + rows * 80;
outputLength += hexDumpLength;
}
else if (bufferFormat == TEXTUAL) {
message = msg.toString(charset);
outputLength += message.length() + 1;
}
StringBuilder buf = new StringBuilder(outputLength)
.append(chStr)
.append(' ')
.append(eventName)
.append(": ")
.append(length)
.append('B');
if (bufferFormat == HEX_DUMP) {
buf.append(NEWLINE);
BufferUtil.appendPrettyHexDump(buf, msg);
}
else if (bufferFormat == TEXTUAL) {
buf.append(' ').append(message);
}
return buf.toString();
}
}
private String formatByteBufHolder(ChannelHandlerContext ctx, String eventName, ByteBufHolder msg) {
String chStr = channelString(ctx.channel());
String msgStr = msg.toString();
ByteBuf content = msg.content();
int length = content.readableBytes();
if (length == 0) {
return new StringBuilder(chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 4)
.append(chStr)
.append(' ')
.append(eventName)
.append(", ")
.append(msgStr)
.append(", 0B")
.toString();
}
else {
StringBuilder buf;
if (bufferFormat != TEXTUAL) {
int outputLength = chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 2 + 10 + 1;
if (bufferFormat == HEX_DUMP) {
int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
int hexDumpLength = 2 + rows * 80;
outputLength += hexDumpLength;
}
buf = new StringBuilder(outputLength)
.append(chStr)
.append(' ')
.append(eventName)
.append(": ")
.append(msgStr)
.append(", ")
.append(length)
.append('B');
if (bufferFormat == HEX_DUMP) {
buf.append(NEWLINE);
appendPrettyHexDump(buf, content);
}
}
else {
String message = content.toString(charset);
int outputLength = chStr.length() + 1 + eventName.length() + 2 + 10 + 2 + message.length();
buf = new StringBuilder(outputLength)
.append(chStr)
.append(' ')
.append(eventName)
.append(": ")
.append(length)
.append("B ")
.append(message);
}
return buf.toString();
}
}
private String formatSimple(ChannelHandlerContext ctx, String eventName, Object msg) {
String chStr = channelString(ctx.channel());
String msgStr = String.valueOf(msg);
return new StringBuilder(chStr.length() + 1 + eventName.length() + 2 + msgStr.length())
.append(chStr)
.append(' ')
.append(eventName)
.append(": ")
.append(msgStr)
.toString();
}
static final char CHANNEL_ID_PREFIX = '[';
static final char CHANNEL_ID_SUFFIX = ']';
static final String ORIGINAL_CHANNEL_ID_PREFIX = "[id: 0x";
static final int ORIGINAL_CHANNEL_ID_PREFIX_LENGTH = ORIGINAL_CHANNEL_ID_PREFIX.length();
static final char TRACE_ID_PREFIX = '(';
}