0

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?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
deepng
  • 809
  • 2
  • 9
  • 20

2 Answers2

1

What is the fully qualified class name of MyLogger? Does the package name start with "com.cf.re"?

You have 2 Loggers configured, root and "com.cf.re". You are only changing the log level of the root logger. If the package of MyLogger starts with "com.cf.re" and you also want to change that Logger's log level, then use this code:

public void setLevel(Level level) {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration conf = ctx.getConfiguration();
    LoggerConfig root = conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    root.setLevel(level);
    LoggerConfig named = conf.getLoggerConfig("com.cf.re");
    named.setLevel(level);
    ctx.updateLoggers(conf);
}
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
1

There is an easier way to do it and it 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:

Configurator.setLevel(logger.getName(), Level.INFO);
Sudeep Shakya
  • 101
  • 1
  • 4