25

In Spring Data R2DBC I can log SQL queries by using

logging.level.org.springframework.data.r2dbc=DEBUG

in the application.properties.

However, this doesn't log the actual values that are bound as query parameters.

How can I log the actual values of query parameters in Spring Data R2DBC?

Harold L. Brown
  • 8,423
  • 11
  • 57
  • 109

5 Answers5

40

This worked for me :

 logging:
  level:
    io.r2dbc.postgresql.QUERY: DEBUG # for queries
    io.r2dbc.postgresql.PARAM: DEBUG # for parameters

I found it here

b.khalsi
  • 551
  • 5
  • 8
  • 4
    It is also described in the `README.md` now: https://github.com/pgjdbc/r2dbc-postgresql/blob/1a9cb75df519c7c5e8fdb706287a085f5b70c51c/README.md#logging – Harold L. Brown Mar 09 '21 at 12:24
  • 2
    Do you happen to know what properties to use in the case of the h2 in-memory database? – Harikesh May 19 '21 at 12:49
5

For h2 and perhaps other DBs try:

logging.level.io.r2dbc.h2=TRACE
Salathiel Genese
  • 1,639
  • 2
  • 21
  • 37
Dmitry Avgustis
  • 854
  • 1
  • 9
  • 14
2

In SpringBoot v2.7.5 make this:

logging.level.org.springframework.r2dbc=TRACE
logging.level.org.springframework.r2dbc=DEBUG
Marcus Voltolim
  • 413
  • 4
  • 12
1

If you happen to use MySQL, you can get a low level network dump of all communication with the database server by setting the driver's log level to TRACE.

logging.level.ROOT=TRACE
logging.level.dev.miku.r2dbc.mysql.client.ReactorNettyClient=TRACE

Example output:

19-11-2020 00:03:28.072 TRACE [reactor-tcp-nio-2] d.m.r.mysql.client.ReactorNettyClient - [id: 0x9cf29125, L:/127.0.0.1:63807 - R:localhost/127.0.0.1:33306] READ: 71B
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 01 00 00 01 01 1b 00 00 02 03 64 65 66 00 00 00 |..........def...|
|00000010| 05 4e 4f 57 28 29 00 0c 3f 00 13 00 00 00 0c 81 |.NOW()..?.......|
|00000020| 00 00 00 00 14 00 00 03 13 32 30 32 30 2d 31 31 |.........2020-11|
|00000030| 2d 31 39 20 30 31 3a 30 33 3a 32 38 07 00 00 04 |-19 01:03:28....|
|00000040| fe 00 00 02 00 00 00                            |.......         |
+--------+-------------------------------------------------+----------------+
Rüdiger Schulz
  • 2,588
  • 3
  • 27
  • 43
0

i have to update source code to log parameterize sql statement in mysql r2dbc . this is my approach :

  Index: src/main/java/dev/miku/r2dbc/mysql/message/client/TextQueryMessage.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/dev/miku/r2dbc/mysql/message/client/TextQueryMessage.java b/src/main/java/dev/miku/r2dbc/mysql/message/client/TextQueryMessage.java
--- a/src/main/java/dev/miku/r2dbc/mysql/message/client/TextQueryMessage.java   (revision bdba9eebe79976aa4bb75aa0aad25c7cbbdf4dec)
+++ b/src/main/java/dev/miku/r2dbc/mysql/message/client/TextQueryMessage.java   (revision 1b5e0133cc8d43bfc32133912d9e3589374ae75a)
@@ -20,6 +20,8 @@
 import dev.miku.r2dbc.mysql.Parameter;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufAllocator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import reactor.core.publisher.Mono;
 
 import java.nio.charset.Charset;
@@ -42,6 +44,8 @@
 
     private final Consumer<String> sqlProceed;
 
+    private static final Logger logger = LoggerFactory.getLogger(TextQueryMessage.class);
+
     public TextQueryMessage(List<String> sqlParts, Parameter[] values, Consumer<String> sqlProceed) {
         requireNonNull(sqlParts, "sql parts must not be null");
         requireNonNull(values, "values must not be null");
@@ -64,6 +68,10 @@
         try {
             Charset charset = context.getClientCollation().getCharset();
             return ParamWriter.publish(sqlParts, values).map(sql -> {
+
+                if(logger.isDebugEnabled()){
+                    logger.debug("parametered sql statement[{}]",sql);
+                }
                 sqlProceed.accept(sql);
 
                 ByteBuf buf = allocator.buffer(sql.length(), Integer.MAX_VALUE);

then change the log level of TextQueryMessage in logback-spring.xml:

<logger name="dev.miku.r2dbc.mysql.message.client.TextQueryMessage" level="DEBUG" additivity="false">
    <appender-ref ref="async_file"/>
</logger>
makingbug
  • 1
  • 1