jOOQ在启用DEBUG日志记录时仍然不记录执行的SQL查询。

huangapple go评论92阅读模式
英文:

jOOQ doesn't log the SQL queries it executes, despite enabling DEBUG logging

问题

我的应用程序使用Logback,并在Logback配置中为org.jooq启用了DEBUG日志记录。尽管如此,jOOQ并未记录SQL查询(它本应该记录的)。可能的原因是什么?

英文:

My application uses Logback and I've enabled DEBUG logging for org.jooq in my Logback configuration. Despite this, jOOQ doesn't log the SQL queries (as it ought to). What can be causing this?

答案1

得分: 1

我假设您指的是使用 org.jooq.tools.LoggerListener 记录日志,默认情况下会以 DEBUG 级别记录所有查询及其结果。

有一个标志可以完全关闭它。默认情况下启用它。您可能已经使用 Settings.executeLogging 将其关闭。

另一个原因可能是 jOOQ 的 org.jooq.tools.JooqLogger 试图加载 org.slf4j.Logger 并通过它记录日志(如果可用),否则会回退到 java.util.logging。请确保在类路径或模块路径上为 jOOQ 提供了 slf4j,并且它由 logback 支持。

英文:

I'm assuming you mean logging with the org.jooq.tools.LoggerListener, which by default DEBUG logs all your queries and their results.

There's a flag to turn that off completely. It's enabled by default. You may have turned it off with Settings.executeLogging.

Another reason may be that jOOQ's org.jooq.tools.JooqLogger tries to classload org.slf4j.Logger and logs through that if available, falling back to java.util.logging otherwise. Do check that you make slf4j available to jOOQ on the classpath or modulepath, and that you have it backed by logback.

答案2

得分: 1

我仔细查看了这个问题(这是我上周遇到的问题),结果我错了。jOOQ确实使用o.j.t.L.o.j.t.LoggerListener记录了查询。问题只是这样的:

  • 我在多个线程上运行不同的查询。

  • 其他查询在这个查询之后被记录。

  • 这个特定的查询返回了2100万条记录,导致了以下问题:

    java.lang.OutOfMemoryError: Java heap space
    正在将堆转储到/tmp/java_pid104782.hprof ...
    
    异常:java.lang.OutOfMemoryError从线程"Thread-0 (ActiveMQ-scheduled-threads)"的UncaughtExceptionHandler抛出
    
    异常:java.lang.OutOfMemoryError从线程"I/O dispatcher 14"的UncaughtExceptionHandler抛出
    堆转储文件创建[48.331秒内的6088668813字节]
    
    异常:java.lang.OutOfMemoryError从线程"req-rsp-timeout-task"的UncaughtExceptionHandler抛出
    
    异常:java.lang.OutOfMemoryError从线程"I/O dispatcher 5"的UncaughtExceptionHandler抛出
    
    异常:java.lang.OutOfMemoryError从线程"lettuce-eventExecutorLoop-1-3"的UncaughtExceptionHandler抛出
    
    异常:java.lang.OutOfMemoryError从线程"I/O dispatcher 11"的UncaughtExceptionHandler抛出
    
  • 由于上述原因,Fetched resultFetched row(s)消息从未被记录。总的来说,很难理解是哪个特定的查询失败了,因为它不是最后一个被记录的查询,可以这么说(因为其他线程的查询)。

我为了减轻这个问题,为正在失败的特定查询添加了一个onRenderEnd处理程序(我使用调试器大致查看了这是哪个jOOQ查询)。使用这个方法,我能更容易地确定了我的特定查询的记录。

我将所有这些细节发布在这里,以帮助其他人和/或将来的我。

英文:

I looked at this a bit more (it was a problem I ran into last week), and it turned out I was wrong. jOOQ did indeed log the queries, using the o.j.t.L.o.j.t.LoggerListener. The problem was just this:

  • I was running different queries on multiple threads.

  • Other queries were being logged after this query.

  • This particular query returned 21 million records, leading to the following:

    java.lang.OutOfMemoryError: Java heap space
    Dumping heap to /tmp/java_pid104782.hprof ...
    
    Exception: java.lang.OutOfMemoryError thrown from the  UncaughtExceptionHandler in thread "Thread-0 (ActiveMQ-scheduled-threads)"
    
    Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "I/O dispatcher 14"
    Heap dump file created [6088668813 bytes in 48.331 secs]
    
    Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "req-rsp-timeout-task"
    
    Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "I/O dispatcher 5"
    
    Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "lettuce-eventExecutorLoop-1-3"
    
    Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "I/O dispatcher 11"
    
  • Because of the above, the Fetched result and Fetched row(s) messages were never logged. And in general, it was hard to understand that it was that specific query that failed, because it wasn't the "last" query to be logged, so to speak (because of the queries from the other threads).

What I did to mitigate this was to add an onRenderEnd handler for the particular query that was failing (I stepped through the code with the debugger to roughly see which jOOQ query this was). Using this, I was able to pin-point the logging for my particular query more easily.

Posting all these details in the help that it might help others and/or my future self.

diff --git a/path/to/MyClass.java b/path/to/MyClass.java
index 92beaa9a91..fcddf9c134 100644
--- a/path/to/MyClass.java
+++ b/path/to/MyClass.java
@@ -30,6 +30,7 @@ import java.util.stream.Stream;
 
 import org.jooq.DSLContext;
 import org.jooq.DatePart;
+import org.jooq.ExecuteListener;
 import org.jooq.Field;
 import org.jooq.Name;
 import org.jooq.Record1;
@@ -417,6 +418,10 @@ public class MyClass {
             // arithmetics on `Field<Instant>` https://stackoverflow.com/a/71508599
             Field<Instant> upperRelatedLimit = field( "(" + seriesTimeFieldName.first() + " + INTERVAL '1 hour')", Instant.class );
 
+            create.configuration().set( ExecuteListener.onRenderEnd( ctx -> {
+                System.out.println( ctx.sql() );
+            } ) );
+
             try ( Stream<AssignedDeviceConnectionState> connectionStates = auditDsl.select(
                     field( "series." + seriesTimeFieldName.first(), Instant.class ).as( "time" ),
                     field( "d.id", Integer.class ).as( "device_id" ),

huangapple
  • 本文由 发表于 2023年6月27日 19:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定