Sep 13, 2018

Using SLF4J / Logback in your Maven Project

Using slf4j:

The only single mandatory dependency is slf4j-api.jar. If no binding is found on the class path, then SLF4J will default to a no-operation implementation.

Sample usage:
package ca.i88.maven; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class App { final Logger logger= LoggerFactory.getLogger(App.class); public static void main( String[] args ) { new App().sample(); } public void sample() { String f="it.i88.ca"; char [] s=new char [] {'i','8','8','.','c','a'}; logger.debug("First one is {}. Second one is {}.", f, s); } }
Add one and only one dependency of the binding of your choice to your pom.xml.

From the client's perspective all versions of slf4j-api are compatible. Client code compiled with slf4j-api-N.jar will run perfectly fine with slf4j-api-M.jar for any N and M. You only need to ensure that the version of your binding matches that of the slf4j-api.jar. You do not have to worry about the version of slf4j-api.jar used by a given dependency in your project.

SLF4J only cares about the formatting anchor, that is the '{' character immediately followed by '}'. Thus, in case your message contains the '{' or the '}' character, you do not have to do anything special unless the '}' character immediately follows '}'. You can escape the '{' character with '\'.

Simple implementation checks for a class loader resource named "simplelogger.properties", and includes any matching definitions from this resource (if it exists).
  • org.slf4j.simpleLogger.logFile - The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err". The simplest case, just set this value and ignore the following.
  • org.slf4j.simpleLogger.defaultLogLevel - Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", or "error"). If not specified, defaults to "info".
  • org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", or "error". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used.
Refer to http://www.slf4j.org/manual.html

Using Logback:

Logback is better than log4j according to its author.
Logback implements SLF4J natively.

So to use SLF4J / Logback in your Maven Project, you only need to add the following:
<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.7</version>
</dependency>
Now put your logback.xml in the following location(s)

src
  • main
    • resources
      • logback.xml
  • test
    • resources
      • logback-test.xml

Sample logback.xml to use:

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

<configuration>
    <appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %caller - %msg%n</Pattern>
        </layout>
    </appender>
    <logger name="ca.i88" level="TRACE"/>
    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>


(refer to:
http://logback.qos.ch/codes.html#layoutInsteadOfEncoder
)

For
Logger logger = LoggerFactory.getLogger(App.class);
logger.debug("Hello i88.ca");
the output is something similar to:
00:18:16.205 [main] DEBUG Caller+0 at com.mycompany.slf.App.main(App.java:16)
 - Hello i88.ca