How do I use logger ConsoleHandler?

In this example you’ll see how to add ConsoleHandler to the Logger instance. It is simple, just create an instance of ConsoleHandler and add it to the Logger using the Logger.addHandler() method.

package org.kodejava.util.logging;

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

public class LogConsoleHandler {
    public static void main(String[] args) {
        // Obtains Logger instance
        Logger logger = Logger.getLogger(LogConsoleHandler.class.getName());

        // Add ConsoleHandler to Logger.
        ConsoleHandler consoleHandler = new ConsoleHandler();
        logger.addHandler(consoleHandler);

        if (logger.isLoggable(Level.INFO)) {
            logger.info("This is information message for testing ConsoleHandler");
        }
    }
}

How do I write log to a file?

An application log need to be persisted so that we can analyze the log when an error occurred in our application. For this reason we need to write to log into a file.

The Logging API provide handlers which helps us to do this. To write log to a file we can use the FileHandler. We define the name of our log file and the appendable mode in this class constructor. For example, you can look at the code presented below.

package org.kodejava.util.logging;

import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.io.IOException;

public class WriteLogToFile {
    public static void main(String[] args) throws IOException {
        Logger logger = Logger.getLogger(WriteLogToFile.class.getName());

        // Create an instance of FileHandler that write log to a file called
        // app.log. Each new message will be appended at the at of the log file.
        FileHandler fileHandler = new FileHandler("app.log", true);
        logger.addHandler(fileHandler);

        if (logger.isLoggable(Level.INFO)) {
            logger.info("Information message");
        }

        if (logger.isLoggable(Level.WARNING)) {
            logger.warning("Warning message");
        }

    }
}

How do I do a conditional logging?

When logging some messages in our application the purpose could be creating a log for debugging purposes. To minimize the impact of these debug message we can use a conditional logging. To do this we need to wrap each call to the logger method with a Logger.isLoggable(Level level) check.

The check will ensure that only logs with the appropriate level will be logged into our application logs. This will automate the on and off our application log without touching the code.

package org.kodejava.util.logging;

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

public class ConditionalLogging {
    private Logger logger =
            Logger.getLogger(ConditionalLogging.class.getName());

    public static void main(String[] args) {
        ConditionalLogging demo = new ConditionalLogging();
        demo.executeMethod();
    }

    // In this method we will check if the Logger level is equals to
    // Level.INFO before we do the real logging. This will minimize
    // the impact of logging if in the next time we increase the level
    // to Level.WARNING or Level.SEVERE so that these message will not
    // be logged anymore.
    public void executeMethod() {
        if (logger.isLoggable(Level.INFO)) {
            logger.info("Entering executeMethod() at : " + new Date());
        }

        // Method body
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(i * j + " ");
            }
            System.out.println();
        }

        if (logger.isLoggable(Level.INFO)) {
            logger.info("Exiting executeMethod() at  : " + new Date());
        }
    }
}

The result of our example code above is shown below:

Oct 07, 2021 8:26:49 PM org.kodejava.util.logging.ConditionalLogging executeMethod
INFO: Entering executeMethod() at : Thu Oct 07 20:26:49 CST 2021
Oct 07, 2021 8:26:49 PM org.kodejava.util.logging.ConditionalLogging executeMethod
INFO: Exiting executeMethod() at  : Thu Oct 07 20:26:49 CST 2021
0 0 0 0 0 
0 1 2 3 4 
0 2 4 6 8 
0 3 6 9 12 
0 4 8 12 16 

How do I log an exception?

In this example you can see how we can log an exception when it occurs. In the code below we are trying to parse an invalid date which will give us a ParseException. To log the exception we call the Logger.log() method, passes the logger Level, add some user-friendly message and the Throwable object.

package org.kodejava.util.logging;

import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class LoggingException {
    private static Logger logger =
            Logger.getLogger(LoggingException.class.getName());

    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        df.setLenient(false);

        try {
            // Try to parse a wrong date.
            Date date = df.parse("12/30/1990");

            System.out.println("Date = " + date);
        } catch (ParseException e) {
            // Create a Level.SEVERE logging message
            if (logger.isLoggable(Level.SEVERE)) {
                logger.log(Level.SEVERE, "Error parsing date", e);
            }
        }
    }
}

The code above will produce the following log message.

Oct 07, 2021 8:21:44 PM org.kodejava.util.logging.LoggingException main
SEVERE: Error parsing date
java.text.ParseException: Unparseable date: "12/30/1990"
    at java.base/java.text.DateFormat.parse(DateFormat.java:399)
    at org.kodejava.util.logging.LoggingException.main(LoggingException.java:20)

How do I check if a message is loggable?

The Logger class provide a setLevel() method to set the logging level. By setting logging to a certain level we can control which message will be logged.

To determine or check if a message will be logged we can use the Logger.isLoggable(Level level) method. Let see the example below.

package org.kodejava.util.logging;

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

public class LoggingLevelCheck {
    public static void main(String[] args) {
        // Obtains an instance of Logger and set the logging level to 
        // Level.WARNING.
        Logger log = Logger.getLogger(LoggingLevelCheck.class.getName());
        log.setLevel(Level.WARNING);

        // Log INFO level message. This message will not be logged due to
        // the log level set to Level.WARNING
        if (log.isLoggable(Level.INFO)) {
            log.info("Application Information Message");
        }

        // Log WARNING level message when Level.WARNING is loggable.
        if (log.isLoggable(Level.WARNING)) {
            log.warning("Application Warning Information");
        }

        // Log SEVERE level message when Level.SEVERE is loggable.
        if (log.isLoggable(Level.SEVERE)) {
            log.severe("Information Severe Information");
        }
    }
}

This will result only the Level.WARNING and Level.SEVERE messages are logged.

Oct 07, 2021 8:18:03 PM org.kodejava.util.logging.LoggingLevelCheck main
WARNING: Application Warning Information
Oct 07, 2021 8:18:03 PM org.kodejava.util.logging.LoggingLevelCheck main
SEVERE: Information Severe Information