Sunday, August 14, 2016

Java Util Logging (jul)

Java Util Logging(jul) is the logging tool which is naturally supported by JRE. It's out of box feature of JRE. So it's very convenient for those applications don't care logging records.
  • JUL API and implementation are inside Java SE API
  • JUL default configuration is inside JRE

Sample code


import java.util.logging.Logger;

public class Demo {
   
    private static final Logger LOGGER = Logger.getLogger(Demo.class.getName());
   
    public static void main(String[] args){
        LOGGER.info("Current Logger = " + LOGGER.getClass().getName());
       
        LOGGER.finest("finest log");
        LOGGER.finer("finer log");
        LOGGER.fine("fine log");
        LOGGER.config("config log");
        LOGGER.info("info log");
        LOGGER.warning("warning log");
        LOGGER.severe("severe log");
    }
   
}

You can directly execute this segment of code.

Default configuration

JUL default configuration adopted by JRE is /$JAVA_HOME/jre/lib/logging.properties. It output messages to console and only output INFO and above levels messages.

Using your own configuration

Create a properties file, named logging.properties

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=FINEST

java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=%h/jul%u.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

Pass this configuration file by jvm system property option, -Djava.util.logging.config.file=/<your_dir>/logging.properties.