logger.setLevel() method is not available in log4j2 API. So how to set log level at run time.
相关问题
- 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
On my side, i had to use this code in order to have this working fine (based on previous answers).
The problem was with the
getLoggerConfig()
call; if the module you are trying to give a new level is not yet registered, this method returns the root logger (or any intermediate sub path registered), and thus instead of altering the level forcom.mycompany
you will alterroot
orcom
level. That's why you have to add a newLoggerConfig
in case the module to alter is not yet registered.Gary Gregory is correct.
Also the answer to this question is right there on the FAQ page in log4j2's site
https://logging.apache.org/log4j/2.x/faq.html#reconfig_level_from_code
Sample Code below:
I'm not sure if this is the best way, but you set the level on org.apache.logging.log4j.core.config.LoggerConfig which you can get from the LoggerContext via the LogManager.
Once set, you can update the loggers with the new configuration.
As an example:
Yields:
Credit to amcintosh, I wrapped their answer in a function:
Despite amoe's comment, this seems to be working correctly for me using Log4J 2.5.
The following APIs in the class org.apache.logging.log4j.core.config.Configurator allow you to change Levels: