Logback is wonderful (Part 1)

Logback is a wonderful logging framework written in Java and distributed under the GNU Lesser General Public License v2.1 and the Eclipse Public License v1.0.

Its functionality can be accessed via the SLF4J API that Logback implements natively. The following toy example demonstrates how to use Logback:

package my;

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

public class Main {

    private static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        logger.error("This is an ERROR message");
        logger.warn("This is a WARNING message");
        logger.info("This is an INFO message");
        logger.debug("This is a DEBUG message");
        logger.trace("This is a TRACE message");
        logger.info("User's name is {}", System.getProperty("user.name"));
    }

}

In order to compile and execute the program logback-core-1.0.3.jar and logback-classic-1.0.3.jar must be in the classpath. (They can be found in the distribution archive.) The JARs are also available from the Maven Central Repository. Simply add the following dependencies to your Apache Maven project that uses Logback:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.3</version>
</dependency>

Logging can be configured via an XML configuration file named logback.xml. To play with the example program use this logback.xml:

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ALL</level>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="ALL">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

Logback will look for it automatically in the classpath.

Logback has great features. It provides a number of appenders to publish log messages. For example, it can deliver log messages in email. You can also write your own appender. Moreover, logging can be fully configured from the XML configuration file.

The next installments of this blog post series will introduce some of its most exciting features.

You can download the above example from here (a more advanced configuration file is included). Building the project requires Apache Maven.

Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: