How can i get all loggers used used in log4j2? In log4j i could use getCurrentLoggers like described here: Number of loggers used
The LoggerContext is the anchor for the logging system. It maintains a list of all the loggers requested by applications and a reference to the Configuration. The Configuration will contain the configured loggers, appenders, filters, etc and will be atomically updated whenever a reconfigure occurs.
Appenders. Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.
The getLogger() method of java. util. logging. LogManager is used to get the specified Logger in this LogManager instance. This Logger must be a named Logger.
Log4j 2 introduced many architectural changes from Log4j 1 including: Components are plugins and use difference interfaces than Log4j 1. Log4j logs Messages instead of Objects. Because Messages render themselves Log4j 2 does not support Renderers. Log4j uses a LoggerContext instead of a LoggerRepository.
get all loggers used in log4j2:
LoggerContext logContext = (LoggerContext) LogManager
.getContext(false);
Map<String, LoggerConfig> map = logContext.getConfiguration()
.getLoggers();
Attention:
use org.apache.logging.log4j.core.LoggerContext
not org.apache.logging.log4j.spi.LoggerContext
YuriR's answer is incomplete in that it does not point that LoggerConfig objects are returned, not Logger. This is the fundamental difference between Log4j1 and Log4j2 - Loggers cannot be dirrectly manipulated in Log4j2. See Log4j2 Architecture for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With