30

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?

3
  • @harold-l-brown did you try logging.level.org.springframework.data.r2dbc=TRACE ? Commented Apr 3, 2020 at 16:31
  • @AbinashGhosh Yes, that didn't print the bindings either. ☹️ Commented Apr 3, 2020 at 18:41
  • 4
    Current Spring versions require a different logger to be set to DEBUG: logging.level.org.springframework.r2dbc=DEBUG Commented Nov 18, 2020 at 22:57

6 Answers 6

50

This worked for me :

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

I found it here

Sign up to request clarification or add additional context in comments.

2 Comments

It is also described in the README.md now: github.com/pgjdbc/r2dbc-postgresql/blob/…
Do you happen to know what properties to use in the case of the h2 in-memory database?
6

In SpringBoot v2.7.5 make this:

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

Comments

5

For h2 and perhaps other DBs try:

logging.level.io.r2dbc.h2=TRACE

Comments

3

There is an r2dbc-proxy project in r2dbc repo that provide exactly what you need.

You can refer to this article about how to configure the proxy.

Basically you will need to provide a ConnectionFactory bean with a proxy on it.

import io.r2dbc.proxy.ProxyConnectionFactory;
import io.r2dbc.proxy.support.QueryExecutionInfoFormatter;
import io.r2dbc.spi.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
class ConnectionConfig {

    @Bean
    ConnectionFactory connectionFactory() {
        ConnectionFactory original = ...
        QueryExecutionInfoFormatter formatter = QueryExecutionInfoFormatter.showAll();
        return ProxyConnectionFactory.builder(original)
                .onAfterQuery(queryInfo -> log.info(formatter.format(queryInfo)))
                .build();
    }
}

And the logging output is like this:

Thread:http-nio-8080-exec-3(38) Connection:3 Transaction:{Create:1 Rollback:0 Commit:0}
Success:True Time:5 Type:Statement BatchSize:0 BindingsSize:1
Query:["INSERT INTO test VALUES ($1)"] Bindings:[($1=100)]

Comments

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                            |.......         |
+--------+-------------------------------------------------+----------------+

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.