How can i get all loggers used used in log4j2? In log4j i could use getCurrentLoggers like described here: Number of loggers used
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
looks like i've found the way:
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.
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
If you are running in a web app, you may have multiple
LoggerContext
s. Please take a look at howLoggerConfig
s are exposed via JMX in the org.apache.logging.log4j.core.jmx.Server class.get all loggers used in log4j2:
Attention:
use org.apache.logging.log4j.core.LoggerContext
not org.apache.logging.log4j.spi.LoggerContext