Main Tutorials

Java Logging APIs Tutorial

This tutorial shows logging using the Java built-in logging APIs in the java.util.logging package.

Table of contents

P.S The java.util.logging is bundled with the Java since JDK 1.4.

1. Java Logging APIs Hello World

1.1 A Java Logging APIs or java.util.logging hello world example, logs some messages and display them in the console.

HelloWorld.java

package com.mkyong;

import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorld {

    private static Logger logger = Logger.getLogger(HelloWorld.class.getName());

    public static void main(String[] args) {

        // set log level to SEVERE, default level info
        //logger.setLevel(Level.SEVERE);

        // Log a info level msg
        logger.info("This is level info logging");

        logger.log(Level.WARNING, "This is level warning logging");

        logger.log(Level.SEVERE, "This is level severe logging");

        System.out.println("Hello Java Logging APIs.");
    }

}

Output

Terminal

Jun 05, 2021 1:35:08 PM com.mkyong.HelloWorld main
INFO: This is level info logging
Jun 05, 2021 1:35:08 PM com.mkyong.HelloWorld main
WARNING: This is level warning logging
Jun 05, 2021 1:35:08 PM com.mkyong.HelloWorld main
SEVERE: This is level severe logging
Hello Java Logging APIs.

2. Logging Levels

2.1 There are 7 logging levels in java.util.logging package :

  • SEVERE (highest value)
  • WARNING
  • INFO (default)
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)

Note
Read the Java logging Level.

2.2 If we set the logging level to SEVERE, it will log messages equal to or above the SEVERE level. And since the SEVERE is the highest logging level, it logs messages which is SEVERE level only.

HelloWorld2.java

package com.mkyong;

import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorld2 {

    private static Logger logger = Logger.getLogger(HelloWorld.class.getName());

    public static void main(String[] args) {

        // set log level to SEVERE, default level info
        logger.setLevel(Level.SEVERE);

        // Log a info level msg
        logger.info("This is level info logging");

        logger.log(Level.WARNING, "This is level warning logging");

        logger.log(Level.SEVERE, "This is level severe logging");

        System.out.println("Hello Java Logging APIs.");
    }

}

Output

Terminal

Jun 05, 2021 1:41:07 PM com.mkyong.HelloWorld main
SEVERE: This is level severe logging
Hello Java Logging APIs.

2.3 If we set the logging level to WARNING, it will log messages equal to or above the WARNING level, which are Level.SEVERE and Level.WARNING.


  // set logging level to WARNING
  logger.setLevel(Level.WARNING);

Output

Terminal

Jun 05, 2021 5:08:11 PM com.mkyong.HelloWorld main
WARNING: This is level warning logging
Jun 05, 2021 5:08:11 PM com.mkyong.HelloWorld main
SEVERE: This is level severe logging
Hello Java Logging APIs.

3. logging.properties

The Java logging APIs (java.util.logging) default loads logging.properties in the $JAVA_HOME/jre/lib/ (Java 8 and before); for Java 9 and above, the logging.properties file moved to $JAVA_HOME/conf.

3.1 Below is a logging.properties sample, which logs messages to both console and a file.

logging.properties

# Logs to file and console
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Global logging levels, 7 levels
.level= SEVERE

# Log file output in user's home directory, %h
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

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

java.util.logging.SimpleFormatter.format=[%1$tc] %4$s: %5$s %n

# log level for package
com.mkyong.level = SEVERE

3.2 We can put the logging.properties in the resources folder (Maven standard directory), and use the system property java.util.logging.config.file to define the location of logging.properties.

logging.properties

LoadLogPropertiesFile.java

package com.mkyong;

import java.util.logging.Level;
import java.util.logging.Logger;

public class LoadLogPropertiesFile {

  static {
      // must set before the Logger
      String path = LoadLogPropertiesFile.class.getClassLoader()
                        .getResource("logging.properties").getFile();
      System.setProperty("java.util.logging.config.file", path);
  }

  private static Logger logger = Logger.getLogger(LoadLogPropertiesFile.class.getName());

  public static void main(String[] args) {

      logger.fine("This is level fine logging");

      // Log a fine level msg
      logger.info("This is level info logging");

      logger.log(Level.SEVERE, "This is level severe logging");

      logger.log(Level.WARNING, "This is level warning logging");

      System.out.println("Hello Java Logging APIs.");
  }

}

Output in console using SimpleFormatter.

Terminal

[Sat Jun 05 14:04:18 MYT 2021] SEVERE: This is level severe logging
Hello Java Logging APIs.

Output in a file using XMLFormatter.

$user_home/java0.log

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2021-06-05T06:04:18.276214600Z</date>
<millis>1622873058276</millis>
<nanos>214600</nanos>
<sequence>0</sequence>
<logger>com.mkyong.LoadLogPropertiesFile</logger>
<level>SEVERE</level>
<class>com.mkyong.LoadLogPropertiesFile</class>
<method>main</method>
<thread>1</thread>
<message>This is level severe logging</message>
</record>
</log>

4. java.util.logging.config.file

The system property java.util.logging.config.file defined the location of logging.properties.

In command line, we can use system property java.util.logging.config.file to define the location of a logging.properties file at runtime.

Terminal

$ java -jar -Djava.util.logging.config.file=/path/logging.properties server.jar

5. Why choose java.util.logging

This Java logging APIs or java.util.logging is part of the Java JDK, suitable for the simple project which required only basic logging features like logs to console or a file.

For more advanced logging features like log file rotation, sending email for error logs, send logs to a database, etc. There are many third-party logging frameworks like the logging facade SLF4j, or logging framework like Logback and Apache Log4j 2.

6. Download Source Code

7. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
alex
2 years ago

Great tutorial, thank you.

Would you consider expanding the logging examples to include:
how to log an exception.
how to log variables in a log message.
how to format a message in json?

Much appreciated

andre
3 months ago

Obviously in the LoadLogPropertiesFile the following comment

// Log a fine level msg
logger.info(“This is level info logging”);

should be
// Log a info level msg

saibabu
1 year ago

such a best & Informative Article 

Thanks & Regards
    V.saibabu

saibabu
1 year ago

very nice Article

  Thanks & Regards
    V.saibabu

saibabu
1 year ago

It is very useful for the students thuse who are preparing

Thanks & Regards
V.saibabu

Daniel Howard
2 years ago

2021 and you document legacy logging. I’m trying to get System.Logger working for JUNIT. How to do that!?