简体   繁体   English

无法找到logback.xml

[英]unable to find logback.xml

I'm trying to use logback as my logger in my simple program but it does not work fine! 我试图在我的简单程序中使用logback作为我的记录器,但它不能正常工作! I put logback/logback.xml and logback/Logback.java in the source directory logback and run through this command line 我将logback / logback.xml和logback / Logback.java放在源目录logback中并运行此命令行

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

which the %CLASSPATH% is an environment variable that has the path of .jar file that logback needs like: 其中%CLASSPATH%是一个环境变量,其路径为.jar文件,该注册需要:

  • logback-access-1.1.2.jar 的logback访问-1.1.2.jar
  • logback-classic-1.1.2.jar 的logback经典-1.1.2.jar
  • logback-core-1.1.2.jar 的logback核-1.1.2.jar
  • slf4j-api-1.7.6.jar SLF4J-API-1.7.6.jar

This is my logback.xml file 这是我的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>

and there is my simple program 还有我的简单程序

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");
    }
}

but unfortunately i just receive log in the console instead of test.log files. 但不幸的是我只是在控制台而不是test.log文件中接收日志。 it seems the logger object just use the default configuration!!! 看来logger对象只是使用默认配置!

If i set the -Dlogback.configurationFile=logback.xml variable as below, it works properly. 如果我按如下所示设置-Dlogback.configurationFile = logback.xml变量,它可以正常工作。 but how to run without this variable? 但是没有这个变量怎么运行?

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

From logback documentation: 从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.

Where should the configuration files such as logback.groovy, logback-test.xml or logback.xml be located on the classpath? 配置文件(如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/.

So you need to put logback.xml in the classpath. 所以你需要将logback.xml放在类路径中。 On one project we had a similar problem although logback.xml was in the right place. 在一个项目中我们遇到了类似的问题,尽管logback.xml位于正确的位置。 Renaming it to logback-test.xml helped. 将其重命名为logback-test.xml有所帮助。

If you add the jar files to C:\\Program Files\\Java\\jre7\\lib\\ext . 如果将jar文件添加到C:\\ Program Files \\ Java \\ jre7 \\ lib \\ ext。 and try to run the program like below: 并尝试运行以下程序:

  • \\logback>java Logback \\ logback> java Logback

it wont see the logback.xml file in the source directory 它不会在源目录中看到logback.xml文件

when i deleted the 当我删除了

  • logback-access-1.1.2.jar 的logback访问-1.1.2.jar
  • logback-classic-1.1.2.jar 的logback经典-1.1.2.jar
  • logback-core-1.1.2.jar 的logback核-1.1.2.jar
  • slf4j-api-1.7.6.jar SLF4J-API-1.7.6.jar

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

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

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