Close

SLF4J with Java Util Logging (JUL) example

[Last Updated: Aug 3, 2017]

This example shows how to use SLF4J with Java Util Logging (JUL) as a logging implementation.

Dependencies

pom.xml

<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-jdk14</artifactId>
   <version>1.8.0-alpha2</version>
</dependency>

JUL configuration

src/main/resources/jul-log.properties

handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$s] %5$s %n

Using SLF4J API

package com.logicbig.example;

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

public class MyClass {
  private static Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

  public static void doSomething() {
      LOGGER.info("a test message");
  }
}
package com.logicbig.example;

public class ExampleMain {

  public static void main(String[] args) {
      String path = ExampleMain.class.getClassLoader()
                                     .getResource("jul-log.properties")
                                     .getFile();
      System.setProperty("java.util.logging.config.file", path);

      MyClass.doSomething();
  }
}

Output

[2017-08-03 16:14:39] [INFO] a test message 

Example Project

Dependencies and Technologies Used:

  • slf4j-jdk14 1.8.0-alpha2: SLF4J JDK14 Binding.
  • JDK 1.8
  • Maven 3.3.9

SLF4J with JUL Example Select All Download
  • slf4j-with-jul-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MyClass.java
          • resources

    See Also