Skip to content

io microsphere logging log4j2 util Log4j2Utils

github-actions[bot] edited this page May 30, 2026 · 5 revisions

Log4j2Utils

Type: Class | Module: microsphere-log4j2 | Package: io.microsphere.logging.log4j2.util | Since: 1.0.0

Source: microsphere-log4j2/src/main/java/io/microsphere/logging/log4j2/util/Log4j2Utils.java

Overview

The utilities class for Log4j2

Declaration

public abstract class Log4j2Utils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.14-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

doInLogger

Log4j2Utils.doInLogger(logger -> logger.setLevel(Level.DEBUG));

addAppender

InMemoryAppender appender = new InMemoryAppender();
  Collection<Logger> loggers = Log4j2Utils.getLoggers();
  Log4j2Utils.addAppender(appender, loggers);

addAppenderForAllLoggers

InMemoryAppender appender = new InMemoryAppender();
  Log4j2Utils.addAppenderForAllLoggers(appender);

removeAppender

InMemoryAppender appender = new InMemoryAppender();
  Collection<Logger> loggers = Log4j2Utils.getLoggers();
  Log4j2Utils.removeAppender(appender, loggers);

removeAppenderForAllLoggers

InMemoryAppender appender = Log4j2Utils.findAppender(InMemoryAppender.NAME);
  Log4j2Utils.removeAppenderForAllLoggers(appender);

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-log4j2</artifactId>
    <version>${microsphere-logging.version}</version>
</dependency>

Tip: Use the BOM (microsphere-logging-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.logging.log4j2.util.Log4j2Utils;

API Reference

Public Methods

Method Description
getConfiguration Get the Configuration
getLogger Get the Logger by specified name
getLogger Get the Logger by the request class
getLevel Get the Level of Logger by the specified logger name
getLevel Get the Level of Logger
setLoggerLevel Set the Level of Logger by the specified logger name
setLoggerLevel Set the Level of Logger
setLoggerLevel Set the Level of Logger
getLevelString Get the Level String of Logger by the specified logger name
getLevelString Get the Level String of Logger
findAppender Find the Appender by its name
getLoggerContext Get the LoggerContext
getLoggers Get all Logger loggers
doInLogger Apply the given Consumer to each Logger in the current LoggerContext.
addAppender Add the given Appender to the specified Logger instances.
addAppenderForAllLoggers Add the given Appender to all Logger instances in the current LoggerContext.
removeAppender Remove the given Appender from the specified Logger instances.
removeAppenderForAllLoggers Remove the given Appender from all Logger instances in the current LoggerContext.

Method Details

getLevel

public static Level getLevel(@Nonnull String loggerName)

Get the Level of Logger by the specified logger name

setLoggerLevel

public static void setLoggerLevel(String loggerName, String levelName)

Set the Level of Logger by the specified logger name

getLevelString

public static String getLevelString(@Nonnull String loggerName)

Get the Level String of Logger by the specified logger name

doInLogger

public static void doInLogger(Consumer<Logger> loggerConsumer)

Apply the given Consumer to each Logger in the current LoggerContext.

Example Usage

`Log4j2Utils.doInLogger(logger -> logger.setLevel(Level.DEBUG));
`

addAppender

public static void addAppender(Appender appender, Iterable<Logger> loggers)

Add the given Appender to the specified Logger instances.

Example Usage

`InMemoryAppender appender = new InMemoryAppender();
  Collection loggers = Log4j2Utils.getLoggers();
  Log4j2Utils.addAppender(appender, loggers);
`

addAppenderForAllLoggers

public static void addAppenderForAllLoggers(Appender appender)

Add the given Appender to all Logger instances in the current LoggerContext.

Example Usage

`InMemoryAppender appender = new InMemoryAppender();
  Log4j2Utils.addAppenderForAllLoggers(appender);
`

removeAppender

public static void removeAppender(Appender appender, Iterable<Logger> loggers)

Remove the given Appender from the specified Logger instances.

Example Usage

`InMemoryAppender appender = new InMemoryAppender();
  Collection loggers = Log4j2Utils.getLoggers();
  Log4j2Utils.removeAppender(appender, loggers);
`

removeAppenderForAllLoggers

public static void removeAppenderForAllLoggers(Appender appender)

Remove the given Appender from all Logger instances in the current LoggerContext.

Example Usage

`InMemoryAppender appender = Log4j2Utils.findAppender(InMemoryAppender.NAME);
  Log4j2Utils.removeAppenderForAllLoggers(appender);
`

See Also

  • LoggerContext
  • Configuration
  • Logger

This documentation was auto-generated from the source code of microsphere-logging.

Clone this wiki locally