Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is getCurrentLoggers's analog in log4j2

How can i get all loggers used used in log4j2? In log4j i could use getCurrentLoggers like described here: Number of loggers used

like image 542
YuriR Avatar asked Jul 07 '13 11:07

YuriR


People also ask

What is LoggerContext in Log4j2?

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.

What are Appenders in Log4j?

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.

What is LogManager getLogger?

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.

What is an equivalent of LoggerRepository in Log4j2?

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.


2 Answers

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

like image 74
zhukunqian Avatar answered Sep 22 '22 18:09

zhukunqian


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.

like image 33
vacant78 Avatar answered Sep 22 '22 18:09

vacant78