Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2/JPA/Hibernate logging is not working

I can't make hibernate log messages with log4j2. It logs only INFO and WARN. On the other side HikariCP works perfectly with this config. Here is the pom.xml:

    ... <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.1</version>
    </dependency> ...

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <!--<Logger name="org.apache.log4j.xml" level="debug"/>-->
        <Root level="info">
            <AppenderRef ref="STDOUT"/>
        </Root>
        <Logger name="org.hibernate" level="debug"/>
        <Logger name="org.hibernate.SQL" level="debug"/>
        <Logger name="com.zaxxer.hikari" level="debug" />
    </Loggers>
</Configuration>
like image 959
Nenad Dordevic Avatar asked Nov 23 '14 10:11

Nenad Dordevic


People also ask

What logger does hibernate use?

Hibernate utilizes Simple Logging Facade for Java (SLF4J) in order to log various system events. SLF4J can direct your logging output to several logging frameworks (NOP, Simple, log4j version 1.2, JDK 1.4 logging, JCL or logback) depending on your chosen binding.

What is root logger in Log4j2?

This concept is known as Logger Hierarchy. Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.


2 Answers

Hibernate logs with jboss-logging. Now Hibernate 4.3.7.Final uses jboss-logging 3.1.3.GA which does not support any binding with log4j2, BUT its last version (3.2.0.Final) already does, so the only thing you need to do is to add the new one:

<!-- HIBERNATE -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.7.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.2.0.Final</version>
</dependency>
<!-- HIBERNATE -->
like image 190
robert Avatar answered Nov 15 '22 18:11

robert


I found the solution. Hibernate definitely uses jboss-logging, so the version coming with hibernate-core and hibernate-entitymanager is 3.1.3.GA and when it gets upgraded to the latest 3.2.0.Final everything has started to work normally.

like image 24
Nenad Dordevic Avatar answered Nov 15 '22 18:11

Nenad Dordevic