1

I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.

 final static Level REQUEST = Level.forName("REQUEST", 450);
 errorLog.info("Showing Requests in logs");
 LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
                        Configuration config = ctx.getConfiguration();
 LoggerConfig loggerConfig = config.getLoggerConfig("console");
 loggerConfig.setLevel(REQUEST);
 LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
 loggerConfigFile.setLevel(REQUEST);
 ctx.updateLoggers();

My configuration file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Properties>
        <Property name="log-path">logs</Property>
    </Properties>
    <Appenders>
        <RollingFile name="file-log" fileName="${log-path}/DataAdapter.log"
        filePattern="${log-path}/DataAdapter-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{MM-dd-yyyy HH:mm:ss} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{MM-dd-yyyy HH:mm:ss} %c{1} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="joy.com.DataAdapter" level="trace" additivity="false">
            <appender-ref ref="file-log" level="info"/>
        </Logger>
        <Root level="trace" additivity="false">
            <appender-ref ref="console" level="info"/>
            <appender-ref ref="file-log" level="info"/>
        </Root>
    </Loggers>
</Configuration>
Tacitus86
  • 1,314
  • 2
  • 14
  • 36
  • Hope this will help: https://stackoverflow.com/a/41993517/2987755 – dkb Nov 09 '18 at 15:22
  • Unfortunately it seems like that is a bit more geared towards command line. – Tacitus86 Nov 09 '18 at 15:33
  • okay, isn't this `log4j2 loggers based on commandline flags` means that, or you can have a look other answers as well in same post: https://stackoverflow.com/a/15922945/2987755 – dkb Nov 09 '18 at 15:34
  • Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code. – Tacitus86 Nov 09 '18 at 15:38
  • That link only works for log4j, not the log4j2 api. – Tacitus86 Nov 09 '18 at 15:54

1 Answers1

0

I finally figured it out. Having the level="" in each appender-ref

 <Root level="trace" additivity="false">
            <appender-ref ref="console" level="info"/>
            <appender-ref ref="file-log" level="info"/>
        </Root>

was overriding my programmatic settings. Removing them allowed me to set the level from the code.

 <Root level="info" additivity="false">
            <appender-ref ref="console"/>
            <appender-ref ref="file-log"/>
        </Root>
Tacitus86
  • 1,314
  • 2
  • 14
  • 36