Is it possible to enable / disable CONSOLE logging depending on a system variable?

We support a system variable with a wide range of applications [debug = true | false], I want to disable all CONSOLE applications in log4j at startup when the system variable debugger is false.

Is the best way to simply programmatically scan applications? Or is there a more elegant approach that I don’t know about, maybe?

Any primer when scanning additives will also be welcome if this is the right approach.

+3
source share
2 answers

I would write a special filter for a console application. Along the line

<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.TTCCLayout">
        <param name="ConversionPattern" value="%d...m%n"/>
    </layout>
    <filter class="OnDebugOnly"/>
</appender>

With filter defined below

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

public class OnDebugOnly extends Filter {

    static boolean debug;

    @Override
    public int decide(LoggingEvent event) {
        return ( debug ? Filter.NEUTRAL : Filter.DENY ) ;
    }
}

, . , . ...
, , .
. ; -)

+2

.properties log4j? log4j , , .. , . "", , .

http://logging.apache.org/log4j/1.2/manual.html

, .

0
source

Source: https://habr.com/ru/post/1788394/


All Articles