Skip to content

Commit 0c6408d

Browse files
committed
Add transport-level logging
[#201]
1 parent c3f1c3b commit 0c6408d

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ R2DBC PostgreSQL Changelog
33

44
0.8.0.RELEASE
55
-----------------------------------------
6+
* Add query logger #201
7+
* Drop SLF4J in favor of Reactor's Logger utilities #200
68
* Fix instant handling #196
79
* Add an auth-config for local connections in testing #195
810
* Invalid listen example in README #192

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,11 @@ If SL4J is on the classpath, it will be used. Otherwise, there are two possible
369369

370370
Logging facilities:
371371

372-
* Driver Logging: `io.r2dbc.postgresql`.
373-
* Query Logging: `io.r2dbc.postgresql.QUERY` on `DEBUG` level.
372+
* Driver Logging (`io.r2dbc.postgresql`)
373+
* Query Logging (`io.r2dbc.postgresql.QUERY` on `DEBUG` level)
374+
* Transport Logging (`io.r2dbc.postgresql.client`)
375+
* `DEBUG` enables `Message` exchange logging
376+
* `TRACE` enables traffic logging
374377

375378
## License
376379
This project is released under version 2.0 of the [Apache License][l].

src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
import io.netty.channel.ChannelDuplexHandler;
2222
import io.netty.channel.ChannelHandlerContext;
2323
import io.netty.channel.ChannelOption;
24+
import io.netty.channel.ChannelPipeline;
2425
import io.netty.channel.EventLoopGroup;
2526
import io.netty.channel.ServerChannel;
2627
import io.netty.channel.epoll.Epoll;
2728
import io.netty.channel.socket.DatagramChannel;
2829
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
30+
import io.netty.handler.logging.LogLevel;
31+
import io.netty.handler.logging.LoggingHandler;
2932
import io.netty.util.ReferenceCountUtil;
33+
import io.netty.util.internal.logging.InternalLogger;
34+
import io.netty.util.internal.logging.InternalLoggerFactory;
3035
import io.r2dbc.postgresql.message.backend.BackendKeyData;
3136
import io.r2dbc.postgresql.message.backend.BackendMessage;
3237
import io.r2dbc.postgresql.message.backend.BackendMessageDecoder;
@@ -303,7 +308,18 @@ public static Mono<ReactorNettyClient> connect(ConnectionProvider connectionProv
303308
tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(connectTimeout.toMillis()));
304309
}
305310

306-
return tcpClient.connect().flatMap(it -> registerSslHandler(sslConfig, it).thenReturn(new ReactorNettyClient(it)));
311+
return tcpClient.connect().flatMap(it -> {
312+
313+
ChannelPipeline pipeline = it.channel().pipeline();
314+
315+
InternalLogger logger = InternalLoggerFactory.getInstance(ReactorNettyClient.class);
316+
if (logger.isTraceEnabled()) {
317+
pipeline.addFirst(LoggingHandler.class.getSimpleName(),
318+
new LoggingHandler(ReactorNettyClient.class, LogLevel.TRACE));
319+
}
320+
321+
return registerSslHandler(sslConfig, it).thenReturn(new ReactorNettyClient(it));
322+
});
307323
}
308324

309325
private static Mono<? extends Void> registerSslHandler(SSLConfig sslConfig, Connection it) {

src/test/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlowTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void executeNoPortalNameSupplier() {
9797
@Test
9898
void executeNoStatement() {
9999
assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.execute(Flux.empty(), NO_OP, () -> "", null, "", false))
100-
.withMessage("statement must not be null");
100+
.withMessage("statementName must not be null");
101101
}
102102

103103
@Test

0 commit comments

Comments
 (0)