如何停止休眠的过度日志记录?

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

How to stop hibernate excessive logging?

问题

<pom.xml>:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.11.2-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.2-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jul</artifactId>
        <version>2.11.2</version>
    </dependency>

<log4j2.xml>:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Configuration status="INFO">
    
        <Appenders>
            <Console name="CONSOLE" target="SYSTEM_OUT">
                <PatternLayout
                    pattern="[%d{dd MMM yyyy HH:mm:ss}] [%-5p] [%t] [%C{1}:%M:%L] - %m%n" />
            </Console>
        </Appenders>
    
        <Loggers>
            <Logger name="com.myapp" level="error" additivity="false">
                <AppenderRef ref="CONSOLE" />
            </Logger>
            <Root level="info">
                <appender-ref ref="CONSOLE" />
            </Root>
            <Logger name="org.hibernate" level="error" additivity="false">
                <AppenderRef ref="CONSOLE" />
            </Logger>
        </Loggers>
    
    </Configuration>

<Hibernate and spring version used>:

    <spring-framework.version>4.0.3.RELEASE</spring-framework.version>
    <hibernate.version>3.6.9.Final</hibernate.version>

<Log its printing>:

    19:05:16.733 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
    19:05:16.734 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: SELECT g FROM myClass g
    19:05:16.734 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: sql sample
    19:05:16.734 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
    19:05:16.734 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG o.hibernate.impl.SessionFactoryImpl - Checking named query: Underlyer.findAll
    19:05:16.735 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG o.h.hql.ast.QueryTranslatorImpl - parse() - HQL: SELECT u FROM entity.class1 u
    19:05:16.737 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.hibernate.hql.ast.AST - --- HQL AST ---
     \-[QUERY] Node: 'query'
        \-[SELECT_FROM] Node: 'SELECT_FROM'
           +-[FROM] Node: 'FROM'
           |  \-[RANGE] Node: 'RANGE'
           |     +-[DOT] Node: '.'
           |     |  +-[DOT] Node: '.'
           |     |  |  +-[DOT] Node: '.'
           |     |  |  |  +-[DOT] Node: '.'
           |     |  |  |  |  +-[DOT] Node: '.'
           |     |  |  |  |  |  +-[DOT] Node: '.'
英文:

My application uses hibernate and its printing lot of logging.
I tried to set logging to error level as below. However, it does not set hibernate logging to error level.
Is anything missing here?

pom.xml :

&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
&lt;version&gt;2.11.2-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
&lt;version&gt;2.11.2-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j-jul&lt;/artifactId&gt;
&lt;version&gt;2.11.2&lt;/version&gt;
&lt;/dependency&gt;

log4j2.xml

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration status=&quot;INFO&quot;&gt;
&lt;Appenders&gt;
&lt;Console name=&quot;CONSOLE&quot; target=&quot;SYSTEM_OUT&quot;&gt;
&lt;PatternLayout
pattern=&quot;[%d{dd MMM yyyy HH:mm:ss}] [%-5p] [%t] [%C{1}:%M:%L] - %m%n&quot; /&gt;
&lt;/Console&gt;
&lt;/Appenders&gt;
&lt;Loggers&gt;
&lt;Logger name=&quot;com.myapp&quot; level=&quot;error&quot; additivity=&quot;false&quot;&gt;
&lt;AppenderRef ref=&quot;CONSOLE&quot; /&gt;
&lt;/Logger&gt;
&lt;Root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
&lt;/Root&gt;
&lt;Logger name=&quot;org.hibernate&quot; level=&quot;error&quot; additivity=&quot;false&quot;&gt;
&lt;AppenderRef ref=&quot;CONSOLE&quot; /&gt;
&lt;/Logger&gt;
&lt;/Loggers&gt;
&lt;/Configuration&gt;

Hibernate and spring version used :

&lt;spring-framework.version&gt;4.0.3.RELEASE&lt;/spring-framework.version&gt;
&lt;hibernate.version&gt;3.6.9.Final&lt;/hibernate.version&gt; 

Log its printing

19:05:16.733 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
19:05:16.734 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: SELECT g FROM myClass g
19:05:16.734 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: sql sample
19:05:16.734 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
19:05:16.734 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG o.hibernate.impl.SessionFactoryImpl - Checking named query: Underlyer.findAll
19:05:16.735 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG o.h.hql.ast.QueryTranslatorImpl - parse() - HQL: SELECT u FROM entity.class1 u
19:05:16.737 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG org.hibernate.hql.ast.AST - --- HQL AST ---
\-[QUERY] Node: &#39;query&#39;
\-[SELECT_FROM] Node: &#39;SELECT_FROM&#39;
+-[FROM] Node: &#39;FROM&#39;
|  \-[RANGE] Node: &#39;RANGE&#39;
|     +-[DOT] Node: &#39;.&#39;
|     |  +-[DOT] Node: &#39;.&#39;
|     |  |  +-[DOT] Node: &#39;.&#39;
|     |  |  |  +-[DOT] Node: &#39;.&#39;
|     |  |  |  |  +-[DOT] Node: &#39;.&#39;
|     |  |  |  |  |  +-[DOT] Node: &#39;.&#39;

答案1

得分: 5

以下是您要翻译的内容:

线程名称似乎暗示您正在运行在 WebLogic 容器上。您确定日志配置是否真的在使用吗?(例如,请参阅 log4j2-not-working-in-weblogic-12-2-1)。

除此之外,我也看不出使用 additivity="false" 然后完全重复相同的 appender 的意义所在。为什么不选择像这样更简单的方式呢?

<Loggers>
    <Root level="info">
        <appender-ref ref="CONSOLE" />
    </Root>
    <Logger name="com.myapp" level="error"/>
    <Logger name="org.hibernate" level="error"/>
</Loggers>
英文:

The thread names seem to suggest that you're running on a weblogic container. Are you sure that the logging config is being used at all? (e.g. see log4j2-not-working-in-weblogic-12-2-1).

Apart from that, I also don't see the point in using additivity="false" and then repeating exactly the same appenders. Why don't you prefer something simpler like this?

&lt;Loggers&gt;
&lt;Root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
&lt;/Root&gt;
&lt;Logger name=&quot;com.myapp&quot; level=&quot;error&quot;/&gt;
&lt;Logger name=&quot;org.hibernate&quot; level=&quot;error&quot;/&gt;
&lt;/Loggers&gt;

答案2

得分: 0

这是一个Spring应用程序的话,你可以通过属性文件选项来阻止SQL打印:

spring.jpa.show-sql=false

如果不是Spring应用程序,你可以尝试以下方法(在Spring应用程序中,这样的Hibernate日志配置是起作用的):

<Logger name="org.hibernate.SQL" level="error"/>
<Logger name="org.hibernate.type" level="error"/>

或者你可以根据需要选择任何日志级别。

英文:

Is it a Spring application if so you could hide SQL from printing via properties file option:

spring.jpa.show-sql=false

If it is not a Spring you could try this (in Sprint apllication such Hibernate log config works):

&lt;Logger name=&quot;org.hibernate.SQL&quot; level=&quot;error&quot;/&gt;
&lt;Logger name=&quot;org.hibernate.type&quot; level=&quot;error&quot;/&gt;

OR whatever Log Level do you want.

答案3

得分: 0

你的记录器似乎没有选择正确的文件配置,可能有两个原因。

  1. 您没有正确指定log4j2.xml文件路径,因为您的项目没有正确选择它,这是从您的日志中检测到的。
  2. 您的log4j2.xml文件在根标签内的appender引用标签中存在错误,请将appender-ref更改为AppenderRef
<Root level="info">
    <AppenderRef ref="CONSOLE" />
</Root>

上述两个确定的原因来自您在log4j2.xml中使用的日志模式,而实际共享的日志完全不同。从log4j2.xml查看您所需的模式布局:

<PatternLayout pattern="[%d{dd MMM yyyy HH:mm:ss}] [%-5p] [%t] [%C{1}:%M:%L] - %m%n" />

它应该打印出类似以下的日志:

[16 Apr 2020 19:05:16.733] [...] [...] [...:...:...] - .......

而实际的日志具有不同的模式:

19:05:16.733 [...] ... ....... - .....

如下所示。

19:05:16.733 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
19:05:16.734 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: SELECT g FROM myClass g
19:05:16.734 [[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: sql sample
英文:

What seems that your logger is not picking the right file configuration, there might be two reasons.

  1. You did not specify your log4j2.xml file path correctly, as your project is not picking it right, which is detected from your logs.
  2. Your log4j2.xml file has some error in appender reference tag inside root tag, change appender-ref to AppenderRef

&lt;Root level=&quot;info&quot;&gt;
&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
&lt;/Root&gt;

to

&lt;Root level=&quot;info&quot;&gt;
&lt;AppenderRef ref=&quot;CONSOLE&quot; /&gt;
&lt;/Root&gt;

The above two identified reasons are from the logging pattern you are using in your log4j2.xml and actual logs shared are totally different. Look your desired pattern layout from log4j2.xml

&lt;PatternLayout pattern=&quot;[%d{dd MMM yyyy HH:mm:ss}] [%-5p] [%t] [%C{1}:%M:%L] - %m%n&quot; /&gt;

It should print logs looks like

[16 Apr 2020 19:05:16.733] [...] [...] [...:...:...] - .......

where actual logs have different patterns.

19:05:16.733 [...] ... ....... - .....

as mentioned under.

19:05:16.733 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
19:05:16.734 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: SELECT g FROM myClass g
19:05:16.734 [[ACTIVE] ExecuteThread: &#39;18&#39; for queue: &#39;weblogic.kernel.Default (self-tuning)&#39;] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: sql sample

答案4

得分: 0

设置Hibernate配置属性:

hibernate.show_sql=false
英文:

Set the hibernate Configuration properties:

hibernate.show_sql=false

huangapple
  • 本文由 发表于 2020年4月6日 16:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61055536.html
匿名

发表评论

匿名网友

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

确定