Change the log level for my appenders programmatically or be influenced from another source.
Dear community.
Log4J2 Version is: 2.12.0
My application is using log4j2.xml and the log level is set to INFO. My application is also reading another configuration file, where I want to put the log level for my users e.g DEBUG.
When the application is initalized (from log4j2.xml), I want change the level for all to DEBUG from second source as an example. So I want override the log4j2.xml INFO to DEBUG from my file (programmatically).
I read some tips and tricks here in the forum about Configurator.setRootLevel, Configurator.setAllLevels etc, but I can't get it working. Like here: Programmatically change log level in Log4j2
Example for testing is:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
public class A {
private static Logger logger = LogManager.getLogger(A.class);
// private static Logger logger = null;
public static void main(String[] args) {
Configurator.setAllLevels(LogManager.getRootLogger().getName(), Level.DEBUG);
logger.info("A INFO message1");
logger.debug("A DEBUG message2");
logger.info("A INFO message3");
}
}
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%d] [%-5p] [%-7t] - %m%n" charset="CP850" />
</Console>
<RollingFile name="RollingFile" fileName="abc.log" filePattern="abc-%d{yyyyMMdd}.log.gz" ignoreExceptions="false">
<PatternLayout>
<Pattern>[%d] [%-5p] [%-7t] - %m [%l]%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="STDOUT" level="INFO" />
<AppenderRef ref="RollingFile" level="INFO"/>
</Root>
</Loggers>
</Configuration>
So I want to get the DEBUG information in my appenders. Any hints?
Thanks in advance.