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");
}
}