Skip to content

Configuring and Formatting Logging

Estimated time to read: 2 minutes

Log Formatting

A log formatter can be used to change the structure of the log message.

There are two formatters built into the library. These are:

  • SimpleFormatter - Human readable way of depicting a log message
  • XMLFormatter - Write the log to standardized XML format

setFormatting

package io.entityfour.logmodule;

import java.util.logging.Logger;

public class LogExample {

    private static final Logger LOGGER = Logger.getLogger(LogExample.class.getName());

    static {
        FileHandler fileHandler = null;
        try { 
            fileHandler = = new FileHandler(<className>.class.getSimpleName() + ".log");
            fileHandler.setFormatting(new SimpleFormatter()); // Set the type of formatter to be used by the handler
        } catch (IOException e){
            e.printStackTrace();
        }
        LOGGER.addHandler(fileHandler);
    }

    public static void main(String[] args) {
        LOGGER.log(Level.FINEST, "This is a mighty fine message"); 
    }
}

Log Filters

Filter is an interface. The isLoggable() method must be implemented. If it returns true for a certain log message, it will be sent to the handler and will be logged. If it returns false, it will be ignored.

A Filter can be applied to the Handler or Logger.

Filter filterAll = arg -> false; // Lamdba argument, arg, returns false
fileHandler.setFilter(filterAll); // Set the Filter of the fileHandler instance to the result of filterAll, which in this case is false. All messages will be dropped.

LogManager and Configuration

The LogManager is the class responsible for managing the logger hierarchy. It loads the configuration for all of the loggers.

Configuration can be done through a special configuration class for a configuration file.

A custom configuration file can be referenced from within the application. If a configuration file is not specified, the default logging properties will be used from the JRE home lib.

Custom Configuration

A custom configuration can be used by placing the configuration in ./src/main/resources/logging.properties

logging.properties
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler // Specify the ConsoleHandler and FileHandler

.level = INFO // Set the level of handlers and loggers

// FileHandler specific properties
java.util.logging.FileHandler.pattern = /logs/<namespace>%u.log // Ensure that this dir exists
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

// ConsoleHandler specific properties
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

// SimpleFormatter specific properties
// java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc] %n // Outputs <level>: <log message> [<date/time>]

Logging Utility Class

LoggingUtil.java
package util;

import java.io.FileInputStream;
import java.util.logging.LogManger

public class LoggingUtil {
    public static void initLogManger() throws IOException {
        LogManager.getLogManager().readConfiguration(new FileInputStream("./src/main/resources/logging.properties"))
    }
}

Log Manager Usage

MainApplicationClassName.java
public class MainApplicationClassName {

    public static void main(String[] args) {
        LoggingUtil.initLogManager();
        // ...
    }
}