Spring 数据 R2DBC 中查询参数的日志值?

Log values of query parameters in Spring Data R2DBC?

Spring Data R2DBC 中,我可以使用

记录 SQL 个查询
logging.level.org.springframework.data.r2dbc=DEBUG

application.properties.

但是,这不会记录绑定为查询参数的实际值。

如何在 Spring Data R2DBC 中记录查询参数的实际值?

如果您碰巧使用 MySQL,您可以通过将驱动程序的日志级别设置为 TRACE.

来获取与数据库服务器的所有通信的低级别网络转储
logging.level.ROOT=TRACE
logging.level.dev.miku.r2dbc.mysql.client.ReactorNettyClient=TRACE

示例输出:

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

这对我有用:

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

找到了here

对于 h2 和其他数据库尝试:

logging.level.io.r2dbc.h2=TRACE

我必须更新源代码以在 mysql r2dbc 中记录参数化 sql 语句。 这是我的方法:

  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);

然后更改logback中TextQueryMessage的日志级别-spring.xml:

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