java 如果 logback 中的条件 - 在两个不同的文件夹中打印日志消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41715211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:05:29  来源:igfitidea点击:

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

javalogback

提问by J2EE Developer

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.

我正在为具有相同日志级别的两个不同文件夹中的日志消息编写代码。我面临的问题是下面的代码无法有条件地打印日志消息(当它变成 else 时)。主要是 else 部分不是在职的。

In simple terms how to write the logs in two different folders based on If else condition using two different appenders.

简单来说,如何根据 If else 条件使用两个不同的 appender 在两个不同的文件夹中写入日志。

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.

请帮助我如何使用其他条件编写日志。提前致谢。

回答by Karthik Prasad

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.

您的配置看起来有误,您甚至在其创建之前就引用了附加程序。如果 atoll logback 支持维护会很乏味,因为您需要为为不同包设置的每个日志级别添加 appender 引用。

Below are the two ways using which you can write to different files.

以下是您可以写入不同文件的两种方法。

  1. By Just changing the file
  1. 只需更改文件
    <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.
  1. 您可以直接在 if 条件下创建 appender。但是你需要先创建 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.

为了使 if else 条件起作用,您需要在类路径中包含 janino.jar,如果您使用的是 maven,则可以添加依赖项。

        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>3.0.6</version>
        </dependency>