Home > database >  How to disable logger in logback.xml
How to disable logger in logback.xml

Time:11-13

I am using logback.xml file to set logging env. Here I have 2 appenders one for console another for file. Console appender will always print the value but the file appender needs to open when required.

Following is my configuration, in which two appender STDOUT and file and both are added root. What I want to control file appender log and want when I change the value of level from OFF to something else then only it should work. Currently if change level OFF then stop both appender and not able to see anything on console.

<configuration>
  <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>

  <appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>/tmp/logback.log</file>
    <append>true</append>
    <immediateFlush>true</immediateFlush>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      </pattern>
    </encoder>
  </appender>

  <logger name="com.demo" level="OFF">
    <appender-ref ref="file" />
  </logger>

  <root level="info">
    <appender-ref ref="file" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Following is the java code.

package com.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
    static Logger loger = LoggerFactory.getLogger(Main.class);
    
    public static void main(String args[]) {
      loger.info(System.getenv("LOGFILE"));
      loger.info("hello");
    }
}

CodePudding user response:

<logger name="com.demo" level="OFF">

This level = "OFF" in the above tag overrides the log level - info of "com.demo" at root level to "OFF"

In a simpler way the moment we declare the level="OFF" to a particular logger, the logger level will be OFF in the whole application and no appender can print the logs for it.

Hence as per my understanding, the best solution would be to use commenting like below:

case1:

<configuration>
  <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>

  <appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>logback.log</file>
    <append>true</append>
    <immediateFlush>true</immediateFlush>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      </pattern>
    </encoder>
  </appender>
  <root level="info">
    <appender-ref ref="file" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

In this case- the log will route to both console and file.

case2:

<configuration>
  <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>

  <appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>logback.log</file>
    <append>true</append>
    <immediateFlush>true</immediateFlush>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      </pattern>
    </encoder>
  </appender>
  <root level="info">
    <!-- <appender-ref ref="file" /> -->
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

In this case- the log will be routed to only the console and no log will be routed to the file appender.

Solution-2:

By taking help of If condition support in logback.xml, we can achieve it.

<root level="info">
    <appender-ref ref="STDOUT" />
    <if condition='property("FILE_APPENDER_ENABLED").contains("true")'>
        <then>
            <appender-ref ref="file" />
        </then>
    </if>
</root>

Configure this property FILE_APPENDER_ENABLED from environment variables, then could enable or disable the file appender.

May be if this solution not suitable to you, but in future it might be useful to someone.

  • Related