Below is my code to change the logging level in Log4j2. I got this from one other stackoverflow thread. It used to work earlier when I didn't have the Log4j2.xml configurator, but for some reason this has stopped working for me after I started using the configurator . The log level does not change at runtime anymore.
public MyLogger() {
Logger log = LogManager.getLogger(MyLogger.class.getName());
}
public void setLevel(Level level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration conf = ctx.getConfiguration();
LoggerConfig config = conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
config.setLevel(level);
ctx.updateLoggers(conf);
}
Here is my Lo4j2.xml configuration file. I got this also too online.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
<Properties>
<Property name="fileName">log/app.log</Property>
<Property name="fileNamePattern">log/app.log-%d{dd-MM-yyyy}-%i.log</Property>
<Property name="logPattern">%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c{2} - %m%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<RollingFile name="MyRollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.cf.re" level="error" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="MyRollingFile"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Can someone please point out the issue here? Or tell me how I can debug this issue?