问题描述
我被迫提出此问题,因为所有其他记录都无法正常工作.
Iam forced to apply this question because everything else documented is not working.
目标:记录在数据库中执行的SQL语句及其有界变量.
Goal: To log SQL statements as they are executed in the DB along with their bounded variables.
环境:Payara 5.182,Hibernate 5.3.2,带有Logback的SLF4j.
Environment: Payara 5.182, Hibernate 5.3.2, SLF4j with Logback.
似乎官方的方法不是触摸persistence.xml来启用休眠日志记录,而是正确配置日志记录框架.
It seems that the official approach is NOT to touch the persistence.xml to enable hibernate logging but to properly configure the logging framework.
pom.xml
<!-- Logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
logback.xml
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="app" level="TRACE"/>
<logger name="org.hibernate.sql" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.hibernate.type.descriptor.sql" level="TRACE">
<appender-ref ref="STDOUT" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
仅用于测试简单查询的演示EJB:
Demo EJB just for testing a simple query:
@PostConstruct
public void init(){
logger.info("Startup EJB. Executing demo query....");
List resultList = em.createQuery("select p from Project p where p.name = ?1")
.setParameter(1, "test")
.getResultList();
int size = resultList.size();
logger.info("Result list size = {}",size);
}
控制台结果
Info: Clustered CDI Event bus initialized
Info: 23:57:31,332 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
23:57:31,333 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
23:57:31,336 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/teohaik/gitProjects/seagle-server/target/seagle-1.0-SNAPSHOT/WEB-INF/classes/logback.xml]
23:57:31,561 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
23:57:31,565 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
23:57:31,613 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
23:57:31,880 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
23:57:31,880 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
23:57:31,880 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
23:57:31,886 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [seagle] to TRACE
23:57:31,886 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.sql] to DEBUG
23:57:31,886 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[org.hibernate.sql]
23:57:31,889 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type.descriptor.sql] to TRACE
23:57:31,889 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[org.hibernate.type.descriptor.sql]
23:57:31,889 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
23:57:31,889 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
23:57:31,889 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
23:57:31,893 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@23351046 - Registering current configuration as safe fallback point
Info: 23:57:31.920 [admin-thread-pool::admin-listener(1)] INFO
g.u.t.s.services.VersionProvider - Startup EJB. Executing demo query....
g.u.t.s.services.VersionProvider - Result list size = 0
Info: Initializing Soteria 1.1-b01 for context '/seagle-server'
Info: Initializing Mojarra 2.4.0-m01.payara-p5 for context '/seagle-server'
Info: Loading application [seagle] at [/seagle-server]
Info: OpenAPI document created.
Info: application was successfully deployed in 12.196 milliseconds.
推荐答案
我在Payara上使用log4j2遇到了同样的问题.经过大量的jboss记录初始化调试(Hibernate内部使用jboss记录)后,我找到了org.jboss.logging.LoggerProviders类.这是jboss日志记录设置目标日志记录提供程序-log4j2,jdk或其他日志记录的起点.特定的日志记录提供程序由系统属性"org.jboss.logging.provider"选择,或者如果目标记录提供程序的类存在于类路径中,则选择这些记录提供程序.
I had the same problem with log4j2 on Payara. After a lot of debugging of jboss logging initialization (Hibernate internally uses jboss logging) I found org.jboss.logging.LoggerProviders class. It is starting point where jboss logging sets target logging provider - log4j2, jdk or other. The particular logging provider is chosen by system property 'org.jboss.logging.provider' or by classes of target logger provider if these are present in the classpath.
但是jboss logger的首次使用来自Payara的Hibernate验证程序,并且Payara委托的类加载器目前在domain/lib文件夹或应用程序包(例如EAR)中看不到任何jar.因此,LoggerProviders类找不到log4j2实现jar文件,并最终使用java.util.Logging
But first use of jboss logger comes from Payara's Hibernate validator and Payara delegated classloader don't see any jars in domain/lib folder nor in application package (e.g. EAR) in this moment. So LoggerProviders class don't find log4j2 implementation jar file and finally uses java.util.Logging
此类加载问题的最终解决方案是将Payara配置'fish.payara.classloading.delegate'设置为false,如下所述 https://payara.gitbooks.io/payara-server/documentation/payara-server/classloading.html 通过EAR或domain/lib中的配置和log4j2 jar,我可以通过log4j2记录SQL.
Final solution of this classloading problem is to set Payara configuration 'fish.payara.classloading.delegate' to false as is described here https://payara.gitbooks.io/payara-server/documentation/payara-server/classloading.htmlWith this confuguration and log4j2 jars in EAR or in domain/lib I am able to log SQL's through the log4j2
这篇关于我们如何使休眠5和payara 5 [glassfish]中的sql语句工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!