3

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

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

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 receive log in the console instead of test.log files. it seems the logger object just use the default configuration!!!

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

2 Answers2

3

From logback 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 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/.

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.

Multisync
  • 8,657
  • 1
  • 16
  • 20
  • \logback>java -cp .;%CLASSPATH% Logback – mohamad mohsen Taheri Oct 14 '14 at 10:11
  • javac -cp slf4j-api-1.5.11.jar;logback-core-1.0.6.jar;logback-classic-1.0.6.jar;logback-access-0.9.19.jar Logback.java – Multisync Oct 14 '14 at 10:28
  • java -cp slf4j-api-1.5.11.jar;logback-core-1.0.6.jar;logback-classic-1.0.6.jar;logback-access-0.9.19.jar;. Logback – Multisync Oct 14 '14 at 10:29
  • @mohamad-mohsen-taheri I put all the files from your question into one directory and run the commands above. The file named "test.log" was created – Multisync Oct 14 '14 at 10:30
  • I tried your versions of jars too (successfully). However, when I renamed "logback.xml" to "logback1.xml" the output was written in console. – Multisync Oct 14 '14 at 10:43
  • I tried this command line in another computer and it works fine but in my own PC it does not work. can anyone help through this problem? i guess, it is some sort of java command line problem – mohamad mohsen Taheri Oct 14 '14 at 10:48
0

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

  • \logback>java Logback

it wont see the logback.xml file in the source directory

when i deleted the

  • 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, it went OK!