I am writing code for log messages in two different folders with the same log level.the problem i am facing is with the below code am not able to print the log messages on conditional(when it becomes else).Mainly the else part is not working.
In simple terms how to write the logs in two different folders based on If else condition using two different appenders.
the code is :
<if condition='property("type").contains("DEV")'>
<then>
<appender-ref ref="FILE-ENGINE" />
</then>
<else>
<appnder-ref ref = "FILE-UI" />
</else>
</if>
The entire configuration file is :
<configuration>
<property name="USER_HOME" value="D:/Log1/" />
<property name="USER_HOME2" value="D:/log2/" />
<if condition='property("type").contains("DEV")'>
<then>
<appender-ref ref="FILE-ENGINE" />
</then>
<else>
<appnder-ref ref = "FILE-UI" />
</else>
</if>
<appender name="FILE-ENGINE" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/${log.name}.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} -
%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-UI" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME2}/DEBUG.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} -
%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-ENGINE-ERROR" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/${log.name}.error</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -
%msg%n</pattern>
</encoder>
</appender>
<logger name="com.code" level="debug" additivity="false">
<appender-ref ref="FILE-ENGINE" />
<appender-ref ref="FILE-UI" />
</logger>
<root level="Error">
<appender-ref ref="FILE-ENGINE-ERROR" />
</root>
</configuration>
pls help me how to write the logs with else condition. Thanks in Advance.
Your configuration looks wrong, you are referring the appender even before its created. If atoll logback supports maintenance would be tedious as you need to add appender refers for each log level you set for different packages.
Below are the two ways using which you can write to different files.
<appender name="fileAppender1" class="ch.qos.logback.core.FileAppender">
<if condition='property("type").contains("DEV")'>
<then>
<file>${USER_HOME}/${log.name}.log</file>
</then>
<else>
<file>${USER_HOME2}/${log.name}.log</file>
</else>
</if>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} -
%msg%n</pattern>
</encoder>
</appender>
<if condition='property("type").contains("DEV")'>
<then>
<appender name="fileAppender1" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME}/${log.name}.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} -
%msg%n</pattern>
</encoder>
</appender>
</then>
<else>
<appender name="fileAppender1" class="ch.qos.logback.core.FileAppender">
<file>${USER_HOME2}/${log.name}.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} -
%msg%n</pattern>
</encoder>
</appender>
</else>
</if>
<root level="DEBUG">
<appender-ref ref="fileAppender1" />
</root>
And for if else condition to work you need to have janino.jar in your classpath, if you are using maven you can added dependency.
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.6</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With