14

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

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
YuriR
  • 1,251
  • 3
  • 14
  • 26

6 Answers6

6

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

zhukunqian
  • 103
  • 4
  • 3
    With 2.6.2, there is no `LoggerContext.getConfiguration()` – Ahmed Ashour Aug 04 '16 at 11:22
  • Use like org.apache.logging.log4j.core.LoggerContext logContext = (org.apache.logging.log4j.core.LoggerContext) LogManager .getContext(false); – zhukunqian Mar 17 '17 at 08:14
  • @zhukunqian No, that's essentially the same and can still result in ```Cannot cast 'org.apache.logging.log4j.simple.SimpleLoggerContext' to 'org.apache.logging.log4j.core.LoggerContext'``` – majixin Nov 04 '22 at 16:07
5

looks like i've found the way:

File configFile = new File("c:\\my_path\\log4j2.xml");
LoggerContext loggerContext = Configurator.initialize("my_config", null, configFile.toURI());
Configuration configuration = loggerContext.getConfiguration();
Collection<LoggerConfig> loggerConfigs = configuration.getLoggers().values();
YuriR
  • 1,251
  • 3
  • 14
  • 26
4

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.

vacant78
  • 163
  • 1
  • 7
2

If you are running in a web app, you may have multiple LoggerContexts. Please take a look at how LoggerConfigs are exposed via JMX in the org.apache.logging.log4j.core.jmx.Server class.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Collection<? extends Logger> loggers = ctx.getLoggers();
danidacila
  • 147
  • 1
  • 3
  • 1
    While this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Apr 07 '16 at 09:36
  • Note that the LoggerContext being casted to is the one for log4j-core - the actual implementation. Although unlikely, Log4j makes no guarantees that these implementation classes won't change from release to release. Also note the javadoc comment for that method: "Whether this collection is a copy of the underlying collection or not is undefined. Therefore, modify this collection at your own risk." In the current code you will get a copy. – rgoers Apr 08 '16 at 01:37
0

It's important to note that LoggerConfig objects are being returned by getLoggers() and NOT the loggers themselves. As vacant78 pointed out, this is only good for setting the configuration for loggers that have yet to be instantiated. If you already have a bunch of loggers already instantiated, changing the configuration using this method does no good.

For example, to change the level of logging at runtime, refer to this link, which utilizes an undocumented API within Apache (no surprise there) that will change all your currently instantiated logger levels: Programmatically change log level in Log4j2

Ketchup201
  • 129
  • 1
  • 9