java Log4j2/JPA/Hibernate 日志记录不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27088083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:11:03  来源:igfitidea点击:

Log4j2/JPA/Hibernate logging is not working

javahibernateloggingormlog4j2

提问by Nenad Dordevic

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:

我无法使用 log4j2 制作休眠日志消息。它只记录 INFO 和 WARN。另一方面,HikariCP 与此配置完美配合。这是 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:

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>

采纳答案by Nenad Dordevic

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.

我找到了解决方案。Hibernate 肯定使用 jboss-logging,所以 hibernate-core 和 hibernate-entitymanager 的版本是 3.1.3.GA,当它升级到最新的 3.2.0.Final 时,一切都开始正常工作了。

回答by robert

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, BUTits last version (3.2.0.Final) already does, so the only thing you need to do is to add the new one:

使用 jboss-logging 休眠日志。现在 Hibernate 4.3.7.Final 使用 jboss-logging 3.1.3.GA,它不支持与 log4j2 的任何绑定,它的最后一个版本(3.2.0.Final)已经支持,所以你唯一需要做的就是添加新的那一个:

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