This article contains Java 9 Logger framework configuration with detail explanation and examples. Java 9 Logger new API with detail explanation.

java.util.logging

Java 9 provide inbuilt logger framework in side package java.util.logging. Reason behind create Logger support in java 9 as under:

  • Defined and used in the java.base module, hence it cannot depend upon the java.util.logging API.
  • Be easily adoptable by applications which use external logging framework, such as SLF4J or Log4J.
  • Reduce dependencies on the java.logging module in order to simplify the JDK module graph.
  • Deals with bootstrap issues, so that platform classes can log messages before the log consumer is initialized.
  • By default, log messages via the java.util.logging API when the java.logging module is present

Let’s see some configuration in java 9 for Logger:

  1. The default configuration is typically loaded from the properties file [JAVA_HOME]/conf/logging.properties in the Java installation directory
  2. If the “java.util.logging.config.file” system property is set, then the property value specifies the properties file to be read as the new configuration. Otherwise, the LogManager default configuration is used.
  3. LOG File will be created at default user home location.
  4. If you want to change log file location then change path in below location in logging.properties file
  5. Java Default Location:
    java.util.logging.FileHandler.pattern = %h/java%u.log
    Modify To
    java.util.logging.FileHandler.pattern = [YOUR_LOCATION]/java%u.log
  6.  NOTE: By default java only configure a ConsoleHandler, which will only show messages at the INFO and above levels. If you want to write LOG on file then need to enable java.util.logging.FileHandler.
  7. Go to logging.properties file and remove comment from
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler

8. java 9 Logger types : SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST (Read for more information)

Example:

  1. module-info.java

    module firstmodule {
       
        requires java.logging;
        exports firstmodule.com.javadeveloperzone;
    }
  2. Main.java

    package firstmodule.com.javadeveloperzone;
    import java.util.logging.Logger;
    public class Main {
        public static void main(String[] args) {
            Logger logger = Logger.getLogger("FirstModule");
            logger.info("JavaDeveloperZone first log.");
            logger.info("JavaDeveloperZone second log.");
        }
    }
    

    If you are using module based development than do not forgot to add dependency of Logging module:

     Output:

    Mar 26, 2017 11:36:57 PM firstmodule.com.javadeveloperzone.Main main
    INFO: JavaDeveloperZone first log.
    Mar 26, 2017 11:36:58 PM firstmodule.com.javadeveloperzone.Main main
    INFO: JavaDeveloperZone second log.
    Disconnected from the target VM, address: '127.0.0.1:50906', transport: 'socket'
    Process finished with exit code 0
    

     java0.log

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE log SYSTEM "logger.dtd">
    <log>
    <record>
      <date>2017-03-26T18:06:23.838177200Z</date>
      <millis>1490551583838</millis>
      <nanos>177200</nanos>
      <sequence>0</sequence>
      <logger>FirstModule</logger>
      <level>INFO</level>
      <class>firstmodule.com.javadeveloperzone.Main</class>
      <method>main</method>
      <thread>1</thread>
      <message>JavaDeveloperZone first log.</message>
    </record>
    <record>
      <date>2017-03-26T18:06:24.192426100Z</date>
      <millis>1490551584192</millis>
      <nanos>426100</nanos>
      <sequence>1</sequence>
      <logger>FirstModule</logger>
      <level>INFO</level>
      <class>firstmodule.com.javadeveloperzone.Main</class>
      <method>main</method>
      <thread>1</thread>
      <message>JavaDeveloperZone second log.</message>
    </record>
    </log>
    

     

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *