无法找到logback.xml

时间:2014-10-14 06:38:44

标签: java xml logback

我试图在我的简单程序中使用logback作为我的记录器,但它不能正常工作! 我将logback / logback.xml和logback / Logback.java放在源目录logback中并运行此命令行

  • \ logback> java -cp .;%CLASSPATH%Logback

其中%CLASSPATH%是一个环境变量,其路径为.jar文件,需要注册:

  • 的logback存取1.1.2.jar
  • 的logback-经典1.1.2.jar
  • 的logback核-1.1.2.jar
  • SLF4J-API-1.7.6.jar

这是我的logback.xml文件

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>test.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>tests.%i.log</fileNamePattern>
            <minIndex>1</minIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>2MB</maxFileSize>
        </triggeringPolicy>

        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>

    </appender>

    <root level="debug">
        <appender-ref ref="FILE" />
    </root>
</configuration>

并且有我的简单程序

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Logback{
    private final static Logger logger = LoggerFactory.getLogger(Logback.class);

    public static void main(String[] args){
        for(int i=0;i<1000000;i++)
            logger.debug("hello");
    }
}

但不幸的是我只是在控制台而不是test.log文件中接收日志。似乎记录器对象只使用默认配置!!!

如果我按如下所示设置-Dlogback.configurationFile = logback.xml变量,它可以正常工作。但如何在没有这个变量的情况下运行?

  • \ logback&gt; java -cp .;%CLASSPATH%-Dlogback.configurationFile = logback.xml Logback

2 个答案:

答案 0 :(得分:3)

来自logback文档:

1. Logback tries to find a file called logback.groovy in the classpath.
2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
3. If no such file is found, it checks for the file logback.xml in the classpath..
4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

logback.groovy,logback-test.xml或logback.xml等配置文件应该位于类路径的哪个位置?

Configuration files such as logback.groovy, logback-test.xml or logback.xml can be located directly under any folder declared in the class path. For example, if the class path reads "c:/java/jdk15/lib/rt.jar;c:/mylibs/" then the logback.xml file should be located directly under "c:/mylibs/", that is as "c:/mylibs/logback.xml". Placing it under a sub-folder of c:/mylibs/, say, c:/mylibs/other/, will not work.

For web-applications, configuration files can be placed directly under WEB-INF/classes/.

因此您需要将logback.xml放在类路径中。在一个项目中我们遇到了类似的问题,尽管logback.xml位于正确的位置。将其重命名为logback-test.xml有帮助。

答案 1 :(得分:0)

如果将jar文件添加到C:\ Program Files \ Java \ jre7 \ lib \ ext。并尝试运行如下程序:

  • \ logback&gt; java Logback

它不会在源目录中看到logback.xml文件

当我删除了

  • logback-access-1.1.2.jar
  • 的logback-经典1.1.2.jar
  • 的logback核-1.1.2.jar
  • SLF4J-API-1.7.6.jar

来自C:\ Program Files \ Java \ jre7 \ lib \ ext的文件,它就可以了!