Introduction to Heirarichal Logging

Hierarchical Logging in Java refers to the parent-child relationship among loggers based on their names. This structure allows you to configure logging behavior at higher levels (e.g., a package or module) and automatically apply or override it in child loggers.

Loggers in java.util.logging are organized in a tree hierarchy, with the dot (.) separator defining different levels of the hierarchya and  similar to package names in Java.

Example Hierarchy:

Consider the following loggers:

Logger.getLogger("com");
Logger.getLogger("com.bank");
Logger.getLogger("com.bank.transaction");
Logger.getLogger("com.bank.transaction.credit");Code language: CSS (css)

This forms the following hierarchy:

Each child logger can:

  • Inherit handlers from its parent unless explicitly disabled via setUseParentHandlers(false)
  • Inherit logging level if not explicitly set

Practical Example

import java.util.logging.*;

public class HierarchicalLoggingExample {
    public static void main(String[] args) {
        Logger parentLogger = Logger.getLogger("com.bank");
        Logger childLogger = Logger.getLogger("com.bank.transaction");

        // Set parent logger
        parentLogger.setLevel(Level.WARNING);
        ConsoleHandler handler = new ConsoleHandler();
        handler.setLevel(Level.ALL);
        handler.setFormatter(new SimpleFormatter());
        parentLogger.addHandler(handler);

        // By default, childLogger inherits parent's level and handler
        childLogger.info("This is an INFO message");     // Not shown (Level < WARNING)
        childLogger.warning("This is a WARNING message"); // Shown
    }
}

Hierarchical logging is a powerful design in Java’s logging framework that:

  • Mirrors Java’s package structure
  • Allows modular, scalable logging configurations
  • Enables parent-child inheritance of logging behavior

It’s a best practice in large applications to use hierarchical logger names for clarity, control, and consistency.

Scroll to Top