SLF4J cookbook

By Maurizio Farina | Posted on September 2017

The Simple Logging Facade for Java (SLF4J) is a logger facage for most logger framework such as log4j, logback and so on.

The following cookbook provides useful information about slf4j using log4j as logger framework.

  • How to include Maven dependencies
  • How to define a logger in a class
  • How to write a log4j.properties

Maven dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.2</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.2</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.2</version>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
</dependency>

How to reate a new logger

1
2
//logger for ReportTest class
private static final Logger log = LoggerFactory.getLogger(ReportTest.class);

The log4j configuration

The Maven dependencies above add Slf4J library to the project but Slf4j is not a logger provider but only a logger facade. This means is not direclty responsible to write logs.

The Maven depencies add log4j appender and log4j library. So, the log provider responsible to write logs in our case is log4j.

log4j library, when starts, need to read a configuration file, if not configuration is added by code. log4j searchs for log4j.properties file in the classpath.

It enough write the configuration file in src/resources to make it available to log4j library.

The following example is log4j.properties file to define loggers and appenders.

the highlighted lines shows how to define the log level for specific namespaces so is possible to have a log levels different for third-party libraries

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
log4j.rootLogger=DEBUG, listfeeds

log4j.appender.listfeeds=org.apache.log4j.RollingFileAppender
log4j.appender.listfeeds.File=target/logs/listfeeds_utils.log
log4j.appender.listfeeds.layout=org.apache.log4j.PatternLayout
log4j.appender.listfeeds.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p [%c:%L] - %m%n
log4j.appender.listfeeds.MaxFileSize=10MB  
log4j.appender.listfeeds.MaxBackupIndex=5 

# =============================================================================
# 3rd Libraries
# =============================================================================
log4j.logger.org.apache=WARN,listfeeds
log4j.logger.org.springframework=INFO,listfeeds

# =============================================================================
# ListFeeds
# =============================================================================
log4j.com.listfeeds=DEBUG, listfeeds

Conversion Pattern

In the log4j.properties file above it's defined the following line:

1
log4j.appender.listfeeds.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p [%c:%L] - %m%n

This configuration line add a conversion pattern to the layout about listfeeds logger. The log4j pattern layout support many conversion character. Take a look to the log4j PatternLayout class.

In our case:

Pattern Description
%d{MM-dd@HH:mm:ss} date/time using ISO8601 format
%-5p log priority; for example: DEBUG, ERROR.
%C fully qualified class name and
%L the line number inside class where is called log statement
%m%n The message and line break.

Output:

1
2
3
4
5
6
7
8
9
09-28@11:38:10 DEBUG [com.listfeeds.TestExport:24] - setUp junit test class
09-28@11:38:10 DEBUG [com.listfeeds.TestExport:26] - indexPath[C:/Users/emaurfa/Dropbox/ListFeeds/environments/production/luceneDB/current]
09-28@11:43:49 DEBUG [com.listfeeds.TestExport:24] - setUp junit test class
09-28@11:43:49 DEBUG [com.listfeeds.TestExport:26] - indexPath[C:/Users/emaurfa/Dropbox/ListFeeds/environments/production/luceneDB/current]
09-28@11:45:20 DEBUG [com.listfeeds.TestExport:24] - setUp junit test class
09-28@11:45:20 DEBUG [com.listfeeds.TestExport:26] - indexPath[C:/Users/emaurfa/Dropbox/ListFeeds/environments/production/luceneDB/current]
09-28@11:47:58 DEBUG [com.listfeeds.TestExport:24] - setUp junit test class
09-28@11:47:58 DEBUG [com.listfeeds.TestExport:26] - indexPath[C:/Users/emaurfa/Dropbox/ListFeeds/environments/production/luceneDB/current]
09-28@11:47:59 DEBUG [com.listfeeds.database.ListFeedsDatabase:56] - IndexWriter created and open index at: [C:/Users/emaurfa/Dropbox/ListFeeds/environments/production/luceneDB/current]