How can we make logging sql statements in hibernate 5 and payara 5 [glassfish] work

隐身守侯 提交于 2021-02-08 10:23:21

问题


Iam forced to apply this question because everything else documented is not working.

Goal: To log SQL statements as they are executed in the DB along with their bounded variables.

Environment: Payara 5.182, Hibernate 5.3.2, SLF4j with Logback.

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

<?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>

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

Result in console

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.

回答1:


Just guessing, but org.hibernate.sql should be org.hibernate.SQL (with capital SQL).

But the second logger org.hibernate.type.descriptor.sql is correct and at least the parameter should be logged. I guess again that hibernate doesn't pick up logback mechanism correctly. As explained in https://www.thoughts-on-java.org/hibernate-logging-guide/ hibernate uses jboss-logging, which routes logging messages to slf4j but I'm not sure if it finds slf4j properly. I suggest removing the slf4j-api dependency as logback provides it already. Maybe that will fix the configuration.




回答2:


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.

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

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.html With this confuguration and log4j2 jars in EAR or in domain/lib I am able to log SQL's through the log4j2



来源:https://stackoverflow.com/questions/51680051/how-can-we-make-logging-sql-statements-in-hibernate-5-and-payara-5-glassfish-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!