找不到logback.xml [英] unable to find logback.xml

查看:391
本文介绍了找不到logback.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use logback as my logger in my simple program but it does not work fine! I put logback/logback.xml and logback/Logback.java in the source directory logback and run through this command line

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

%CLASSPATH%是一个环境变量,该环境变量具有登录所需的.jar文件的路径,例如:

which the %CLASSPATH% is an environment variable that has the path of .jar file that logback needs like:

  • logback-access-1.1.2.jar
  • logback-classic-1.1.2.jar
  • logback-core-1.1.2.jar
  • slf4j-api-1.7.6.jar

这是我的logback.xml文件

This is my logback.xml file

<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文件.看来记录器对象只是使用默认配置!!!

but unfortunately i just receive log in the console instead of test.log files. it seems the logger object just use the default configuration!!!

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

If i set the -Dlogback.configurationFile=logback.xml variable as below, it works properly. but how to run without this variable?

  • \ logback> java -cp.;%CLASSPATH%-Dlogback.configurationFile = logback.xml 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等配置文件放在何处?

Where should the configuration files such as logback.groovy, logback-test.xml or logback.xml be located on the classpath?

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很有帮助.

So you need to put logback.xml in the classpath. On one project we had a similar problem although logback.xml was in the right place. Renaming it to logback-test.xml helped.

这篇关于找不到logback.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆