前段时间项目需要spring cloud gateway打印请求报文等信息,一开始是使用Wiretap Logger。
在application.yml配置文件增加
spring:
cloud:
gateway:
httpclient:
wiretap: true
输出日志如下:
83152 [reactor-http-nio-2] DEBUG r.netty.http.server.HttpServer - [id: 0x2c9d1b8b, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:55396] WRITE: 115B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d |HTTP/1.1 200 OK.|
|00000010| 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 |.Content-Type: t|
|00000020| 65 78 74 2f 68 74 6d 6c 3b 63 68 61 72 73 65 74 |ext/html;charset|
|00000030| 3d 55 54 46 2d 38 0d 0a 43 6f 6e 74 65 6e 74 2d |=UTF-8..Content-|
|00000040| 4c 65 6e 67 74 68 3a 20 31 31 0d 0a 44 61 74 65 |Length: 11..Date|
|00000050| 3a 20 54 75 65 2c 20 31 31 20 4d 61 79 20 32 30 |: Tue, 11 May 20|
|00000060| 32 31 20 30 37 3a 34 35 3a 33 34 20 47 4d 54 0d |21 07:45:34 GMT.|
|00000070| 0a 0d 0a |... |
+--------+-------------------------------------------------+----------------+
83156 [reactor-http-nio-2] DEBUG r.netty.http.server.HttpServer - [id: 0x2c9d1b8b, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:55396] WRITE: 11B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 20 77 6f 72 6c 64 |hello world |
+--------+-------------------------------------------------+----------------+
这样的日志很难阅读,不利于解决问题,然后自定义实现netty的logginghandler类,使之输出我们需要的日志。
改动如下:
@Slf4j
public class LoggingHandler extends ChannelDuplexHandler{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if ( msg instanceof ByteBuf ){
ByteBuf buf = (ByteBuf) msg;
if ( buf.readableBytes()>0 ){
final String data = buf.toString(StandardCharsets.UTF_8);
log.info(data);
}
}
super.write(ctx,msg,promise);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if ( msg instanceof ByteBuf ){
ByteBuf buf = (ByteBuf) msg;
if ( buf.readableBytes()>0 ){
final String data = buf.toString(StandardCharsets.UTF_8);
log.info(data);
}
}
super.channelRead(ctx,msg);
}
}
@Configuration
public class LoggingConfiguration implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof HttpClient) {
HttpClient client = (HttpClient) bean;
return client.tcpConfiguration(tcpClient -> tcpClient.bootstrap(b -> BootstrapHandlers.updateConfiguration(b, "log", ((connectionObserver, channel) -> channel.pipeline().addFirst("log", new LoggingHandler())))));
}
return bean;
}
}
展现效果:
80015 [reactor-http-nio-4] INFO c.l.sidecar.example.LoggingHandler - GET /hello HTTP/1.1
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: _ga=GA1.1.270405173.1557761261
Forwarded: proto=http;host="localhost:8080";for="0:0:0:0:0:0:0:1:55396"
X-Forwarded-For: 0:0:0:0:0:0:0:1
X-Forwarded-Proto: http
X-Forwarded-Port: 8080
X-Forwarded-Host: localhost:8080
host: localhost:8761
content-length: 0
83108 [reactor-http-nio-4] INFO c.l.sidecar.example.LoggingHandler - HTTP/1.1 200
Content-Type: text/html;charset=UTF-8
Content-Length: 11
Date: Tue, 11 May 2021 07:45:34 GMT
hello world