SLF4J with JDK14 Example

SLF4J binding with JDK14 (slf4j-jdk14)

Now we can experiment and swap different logger implementations, but your application code can remain the same. All we need is to add slf4j-jdk14 with another popular logger implementation, such as the Log4j.We can use a Java class to configure the Java Logging API. You do so by specifying the name of the class in the JVM parameter java.util.logging.config.class. It is the constructor of that class that should load the configuration and apply it to the Logger's in the hierarchy.

package com.javavillage.slf4j;

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

public class SLF4JSample {

	private static Logger LOGGER = LoggerFactory.getLogger(SLF4JSample.class);
    public static void main(String[] args) {
        LOGGER.info("Hi, Welcome to SLF4J exmaple");
    }

}
for now I am adding below dependency for maven pom.xml
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.13</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-jdk14</artifactId>
	<version>1.7.13</version>
</dependency>
logging.properties
Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such as src/main/resources/logging.properties, but you would also need to add a System properties -Djava.util.logging.config.file=logging.properties in order to have it pick it up. Here is an example to get you started:
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
 
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=F:\\logs\\jetty.log
 
# Write 10MB before rotating this file
java.util.logging.FileHandler.limit=10000000
 
# Number of rotating files to be used
java.util.logging.FileHandler.count=4
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
 
.level=INFO
project structure after maven build.

SLF4J with JDK14 workspace

Now you see logging output on STDOUT with INFO level..

Dec 13, 2015 3:58:38 PM com.javavillage.slf4j.SLF4JSample main
INFO: Hi, Welcome to SLF4J exmaple