Search code examples
javalogginglog4jlogbackslf4j

JAVA: Configure logback in standalone apps


I have a Java standalone app. with a main method, with these 2 imports:

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

and

private static final Logger log = LoggerFactory.getLogger(PecadorDeLaPradera.class);

In the same folder I also have a logback.xml but I don't know how to tell the program that uses the logback.xml to config the log

I couldn't find any class similar to org.apache.log4j.PropertyConfigurator

I have this logback.xml in the same folder of the Java class I am running:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- trace, debug, info, warn, error, fatal -->
    <timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>  


    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <!-- To enable JMX Management -->
    <jmxConfigurator/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{"yyyy-MM-dd HH:mm"}  [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>pecador.log</file> 

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

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>1MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>



    <!-- <logger name="org.springframework.orm.jpa"     level="debug" /> -->    
    <logger name="com.calzada.pecador"  level="debug" />


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

I also run the appl. with Program arguments:

-Dlogback.configurationFile=/Users/calzada/Dev/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml

and I got this error:

java.util.MissingResourceException: Can't find bundle for base name -Dlogback.configurationFile=/Users/nullpointer/Development/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml, locale en_ES

The library used is logback-classic-1.2.3.jar


Solution

  • Here's how logback configures itself:

    Logback tries to find a file called logback-test.xml in the classpath.

    If no such file is found, logback tries to find a file called logback.groovy in the classpath.

    If no such file is found, it checks for the file logback.xml in the classpath.

    If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.

    If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

    You can also point Logback at a specific configuration file using the system parameter logback.configurationFile. From the docs:

    You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

    java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1

    All the above taken from the docs.

    According to your question you have a logback.xml but this file is in the "same folder" as your class. Unless your class is in the root package this means that logback.xml is not in the root of the classpath so Logback will not discover it. In order for Logback to configure itself from this file you can do one of the following:

    • Place your logback.xml in the root of your classpath (for a Maven project this is as simple as copying logback.xml to src/main/resources)
    • Run your Java program with -Dlogback.configurationFile=/path/to/logback.xml