7

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.

Michu93
  • 5,058
  • 7
  • 47
  • 80
J2EE Developer
  • 105
  • 1
  • 1
  • 7
  • Does this answer your question? [logback conditional logging](https://stackoverflow.com/questions/11017148/logback-conditional-logging) – 030 May 22 '20 at 13:30

1 Answers1

10

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.

  1. By Just changing the file
    <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>
  1. You can directly create appenders in if condition. however you need to create appenders first itself.
    <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>
Betlista
  • 10,327
  • 13
  • 69
  • 110
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112
  • Hi Karthik thanks for info. if i use different appender class in else part (like) ch.qos.logback.core.rolling.RollingFileAppender is not working i.e not writing the logs. can you tell me help me on this. – J2EE Developer Jan 23 '17 at 06:57
  • @J2EEDeveloper could you tell how did you set this property type. – TeamZ Nov 22 '19 at 05:19
  • @karthik could you tell me how to set property type `````` in java – TeamZ Nov 22 '19 at 06:27
  • is there any documenation on your answer? How do I compare two variables? – john k Apr 06 '22 at 20:35