使用slf4j(log4j.xml)的日志文件为空

时间:2019-01-16 20:41:34

标签: java eclipse log4j slf4j

在我的项目中,我决定使用slf4j记录控制台和文件,但是我的日志文件始终为空。有人可以发现我的错误吗? 使用commons-logging一切正常,但使用起来不方便。 Slf4j 1.7.25版本

UPD:通过更改构建路径中的库(slf4j-simple-1.7.25而不是slf4j-log4j12-1.7.25)解决了该问题

WebPageParser.java

import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.xml.DOMConfigurator;

public class WebPageParser {
    public static void main(String[] args) {
        runLogger();
        Logger logger = LoggerFactory.getLogger(WebPageParser.class);
        logger.info("Application is running...");
    }

    private static void runLogger() {
        String projectLocation = System.getProperty("user.dir");
        DOMConfigurator.configure(projectLocation + "\\src\\Resources\\log4j.xml");
    }
}

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="file"
    class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="log.log" />
    <param name="append" value="true" />
    <param name="maxFileSize" value="1MB" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>
<appender name="console"
    class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>

<root>
    <level value="INFO" />
    <appender-ref ref="file" />
    <appender-ref ref="console" />
</root>

这是调试log4j的输出:

log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [INFO].
log4j: root level set to INFO
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [file] to [log.log].
log4j: Setting property [append] to [true].
log4j: Setting property [maxFileSize] to [1MB].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: log.log, true
log4j: setFile ended
log4j: Adding appender named [file] to category [root].
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].

0 个答案:

没有答案