Could not find logback.xml

I am trying to use logback as my logger in my simple program, but it doesn't work fine! I put logback / logback.xml and logback / Logback.java in the original directory and skipped this command line

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

which% CLASSPATH% is an environment variable that has the path to the .jar file that is needed to restore the log:

  • Logback access 1.1.2.jar
  • Logback-classic 1.1.2.jar
  • Logback-core-1.1.2.jar
  • Slf4j-api-1.7.6.jar

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>

      

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 get console input instead of test.log files. it seems that the logger object is just using the default config !!!

If I set the variable -Dlogback.configurationFile = logback.xml as shown below, it works correctly. but how to work without this variable?

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


source to share


2 answers


From the log documentation:

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 config files like logback.groovy, logback-test.xml or logback.xml be stored in 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/.

      

So, you need to put logback.xml on your classpath. In one project, we had a similar problem, although the logback.xml was in the right place. Renaming it to logback-test.xml did the trick.

+3


source


If you add jar files to C: \ Program Files \ Java \ jre7 \ lib \ ext. and try to run the program like below:

  • \ logback> java Logback

it won't see the logback.xml file in the source directory



when i deleted

  • logback-access-1.1.2.jar
  • Logback-classic 1.1.2.jar
  • Logback-core-1.1.2.jar
  • Slf4j-api-1.7.6.jar

files from C: \ Program Files \ Java \ jre7 \ lib \ ext, everything went fine!

0


source







All Articles