英文:
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 :
<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: '.'
答案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?
<Loggers>
<Root level="info">
<appender-ref ref="CONSOLE" />
</Root>
<Logger name="com.myapp" level="error"/>
<Logger name="org.hibernate" level="error"/>
</Loggers>
答案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):
<Logger name="org.hibernate.SQL" level="error"/>
<Logger name="org.hibernate.type" level="error"/>
OR whatever Log Level do you want.
答案3
得分: 0
你的记录器似乎没有选择正确的文件配置,可能有两个原因。
- 您没有正确指定log4j2.xml文件路径,因为您的项目没有正确选择它,这是从您的日志中检测到的。
- 您的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.
- You did not specify your log4j2.xml file path correctly, as your project is not picking it right, which is detected from your logs.
- Your log4j2.xml file has some error in appender reference tag inside root tag, change appender-ref to AppenderRef
<Root level="info">
<appender-ref ref="CONSOLE" />
</Root>
to
<Root level="info">
<AppenderRef ref="CONSOLE" />
</Root>
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
<PatternLayout pattern="[%d{dd MMM yyyy HH:mm:ss}] [%-5p] [%t] [%C{1}:%M:%L] - %m%n" />
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: '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
答案4
得分: 0
设置Hibernate配置属性:
hibernate.show_sql=false
英文:
Set the hibernate Configuration properties:
hibernate.show_sql=false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论