简体   繁体   English

使用slf4j和log4j进行休眠日志记录不会生成日志文件

[英]Hibernate logging with slf4j and log4j not generating logfile

I'm trying to config my hibernate application to output logging information. 我正在尝试配置我的休眠应用程序以输出日志记录信息。 I have my log4j.properties configured as follows: 我将log4j.properties配置如下:

{
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\sisco.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=INFO, file, stdout, debug


# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO

# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL

}

But I think hibernate is not even reading this file, because whatever I set to this file, nothing changes, and also I deleted it, and my application ran normally. 但是我认为休眠状态甚至都没有读取该文件,因为无论我对此文件设置了什么,都没有改变,并且删除了它,并且我的应用程序正常运行。

I think that maybe, some other jars in the project might have a log4j.properties file included in the jar, and then hibernate is using it. 我认为,也许项目中的其他一些jar可能在jar中包含一个log4j.properties文件,然后hibernate正在使用它。 Does it make any sense? 有什么意义吗?

Does anyone have any idea? 有人有什么主意吗?

Thanks 谢谢

you have to add this jar into your classpath 您必须将此罐子添加到您的类路径中

slf4j-api-1.7.5.jar       // the slf4j API
slf4j-log4j12-1.7.5.jar   // log4j bindings for slf4j
log4j-1.2.17.jar          // log4j itself

that's work for me, good luck ~ 那对我有用,祝你好运〜

The file log4j.xml has higher priority than the file log4j.properties . 文件log4j.xml优先级高于文件log4j.properties优先级。

eg: 例如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" 
                                     "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- console -->
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="TRACE" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="conversionPattern"
                value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
        </layout>
    </appender>

    <!-- categories -->
    <category name="org.hibernate">
        <priority value="INFO" />
    </category>
    <category name="org.hibernate.type">
        <priority value="TRACE" />
    </category>

    <!-- root -->
    <root>
        <priority value="TRACE" />
        <appender-ref ref="STDOUT" />
    </root>
</log4j:configuration>

Since version 1.2.12 of the framework, just need to put it in the default package in the src folder of your project. 从框架的1.2.12版本开始,只需将其放在项目的src文件夹中的默认包中即可。 log4j automatically load it. log4j自动加载它。 No need to load it programmatically or anything extra. 无需以编程方式或其他方式加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM