Java LoggerNameUtil-class And Method Code Example


Here is an example of how you can use the LoggerNameUtil class from the logback.classic package:

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.LoggerNameUtil;
import org.slf4j.LoggerFactory;

public class MyClass {
  public static void main(String[] args) {
    // Get the logger
    Logger logger = (Logger) LoggerFactory.getLogger(MyClass.class);

    // Get the logger context
    LoggerContext context = logger.getLoggerContext();

    // Get the logger name
    String loggerName = logger.getName();

    // Check if the logger name is a root logger name
    boolean isRoot = LoggerNameUtil.isRoot(context, loggerName);
    System.out.println(isRoot);  // prints false

    // Get the root logger name
    String rootLoggerName = LoggerNameUtil.getRootLoggerName(context);
    System.out.println(rootLoggerName);  // prints "ROOT"
  }
}

The LoggerNameUtil class is a utility class that provides methods for working with logger names. In this example, we use the isRoot method to check if the logger name is a root logger name, and the getRootLoggerName method to get the root logger name.

The isRoot method takes a LoggerContext and a logger name as arguments and returns a boolean indicating whether the logger name is a root logger name. The getRootLoggerName method takes a LoggerContext as an argument and returns the root logger name.