Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if condition in logback - print log messages in two different folders

Tags:

java

logback

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.

like image 253
J2EE Developer Avatar asked Jan 18 '17 09:01

J2EE Developer


1 Answers

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>
like image 137
Karthik Prasad Avatar answered Sep 30 '22 14:09

Karthik Prasad