duke_42
duke_42

Reputation: 39

how can i switch on/off an appender in logback.xml with java

i want to switch on/off an appender for stdout with javacode.

this is a snippet of logback.xml:

 <root level="error">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</root>
 <if condition='property("log2console").contains("true")'>
    <then>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${PATTERN}</pattern>
            </encoder>
        </appender>
                    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  <level>debug</level>
</filter>

if i set the property before i start the application, it works. but if the application is started and i change the property "log2Console = true", it dosn´t work.

i use a text-file. to set the logging. the first entry should enable the log2Console and the second entry set the loglevel. if i start the application the loglevel is working but not the enable "log2console" i try different:

LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

        try (BufferedReader br = new BufferedReader(new FileReader(log2Console))) {

            String sCurrentLine;
            boolean Enablelog2Console = false;
            while ((sCurrentLine = br.readLine()) != null) {
                if (sCurrentLine.equals("true")) {
                    Enablelog2Console = true;

                    Properties prop = new Properties();

                    //try with different optionen
                    prop.setProperty("log2console", "true");
                    lc.putProperty("log2console", "true");

                }
                if (Enablelog2Console) {
                    Logger rootLOG = (Logger) org.slf4j.LoggerFactory
                        .getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
                    if (sCurrentLine.equals("trace")) {
                        ((ch.qos.logback.classic.Logger) rootLOG).setLevel(Level.TRACE);

                    } else if (sCurrentLine.equals("debug")) {
                        ((ch.qos.logback.classic.Logger) rootLOG).setLevel(Level.DEBUG);
                    } else if (sCurrentLine.equals("info")) {
                        ((ch.qos.logback.classic.Logger) rootLOG).setLevel(Level.INFO);
                    } else if (sCurrentLine.equals("warn")) {
                        ((ch.qos.logback.classic.Logger) rootLOG).setLevel(Level.WARN);
                    } else if (sCurrentLine.equals("error")) {
                        ((ch.qos.logback.classic.Logger) rootLOG).setLevel(Level.ERROR);
                    }
                    System.out.println("das LogLevel wurde auf " + sCurrentLine + " eingestellt.");
                }
            }

        } catch (IOException e) {

Upvotes: 0

Views: 1671

Answers (2)

manasouza
manasouza

Reputation: 1225

The structure of your logback.xml should be something like this:

<configuration>

    <if condition='isDefined("otherlog")'>
        <then>
            <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
                <!-- Optional : filter logs at or above a level -->
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>INFO</level>
                </filter>
        ...
            </appender>
            <root level="debug">
                <appender-ref ref="CLOUD"/>
            </root>
        </then>
        <else>
            <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
                </encoder>
            </appender>

            <root level="INFO">
                <appender-ref ref="STDOUT"/>
            </root>

        </else>
    </if>

</configuration>

then if you run your application with -Dotherlog=true it will use com.google.cloud.logging.logback.LoggingAppender, otherwise it will use ch.qos.logback.core.ConsoleAppender

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32550

You need to use scan attribute to force logback to reconfigure itself every X seconds eg:

<configuration scan="true" scanPeriod="30 seconds" >

More can be found here http://logback.qos.ch/manual/configuration.html#autoScan

Upvotes: 0

Related Questions