

This article contains Java 9 Logger framework configuration with detail explanation and examples. Java 9 Logger new API with detail explanation.
Table of Contents
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 thejava.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 thejava.logging
module is present
Let’s see some configuration in java 9 for Logger:
- The default configuration is typically loaded from the properties file [JAVA_HOME]/conf/logging.properties in the Java installation directory
- 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.
- LOG File will be created at default user home location.
- If you want to change log file location then change path in below location in logging.properties file
Java Default Location: java.util.logging.FileHandler.pattern = %h/java%u.log Modify To java.util.logging.FileHandler.pattern = [YOUR_LOCATION]/java%u.log
- 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.
- 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:
module-info.java
module firstmodule { requires java.logging; exports firstmodule.com.javadeveloperzone; }
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?
Let us know if you liked the post. That’s the only way we can improve.