当前位置: 首页 > 工具软件 > KIF > 使用案例 >

logback.xml if 条件判断

邓高韵
2023-12-01

本地开发需要打印控制台日志,而测试以及生产环境则不需要,此时可在logback.xml中通过配置文件值进行条件判断。


1.先上代码

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
	
	<springProperty scope="context" name="env" source="deploy.env"/>
	
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		...
		...
	</appender>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		...
		...
	</appender>
	
	<root level="INFO">
        <appender-ref ref="FILE"/>
        <if condition='property("env").contains("local")'>
            <then>
                <!-- this only enable in local -->
                <appender-ref ref="STDOUT"/>
            </then>
        </if>
    </root>

application.properties

deploy.env=local

pom.xml

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

2.文档

Developers often need to juggle between several logback configuration files targeting different environments such as development, testing and production. These configuration files have substantial parts in common differing only in a few places. To avoid duplication, logback supports conditional processing of configuration files with the help of , and elements so that a single configuration file can adequately target several environments. Note that conditional processing requires the Janino library.

The general format for conditional statements is shown below.

   <!-- if-then form -->
   <if condition="some conditional expression">
    <then>
      ...
    </then>
  </if>
  
  <!-- if-then-else form -->
  <if condition="some conditional expression">
    <then>
      ...
    </then>
    <else>
      ...
    </else>    
  </if>

The condition is a Java expression in which only context properties or system properties are accessible. For a key passed as argument, the property() or its shorter equivalent p() methods return the String value of the property. For example, to access the value of a property with key “k”, you would write property(“k”) or equivalently p(“k”). If the property with key “k” is undefined, the property method will return the empty string and not null. This avoids the need to check for null values.

The isDefined() method can be used to check whether a property is defined. For example, to check whether the property “k” is defined you would write isDefined(“k”) Similarly, if you need to check whether a property is null, the isNull() method is provided. Example: isNull(“k”).

<configuration debug="true">

  <if condition='property("HOSTNAME").contains("torino")'>
    <then>
      <appender name="CON" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
          <pattern>%d %-5level %logger{35} - %msg %n</pattern>
        </encoder>
      </appender>
      <root>
        <appender-ref ref="CON" />
      </root>
    </then>
  </if>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${randomOutputDir}/conditional.log</file>
    <encoder>
      <pattern>%d %-5level %logger{35} - %msg %n</pattern>
   </encoder>
  </appender>

  <root level="ERROR">
     <appender-ref ref="FILE" />
  </root>
</configuration>

Conditional processing is supported anywhere within the element. Nested if-then-else statements are also supported. However, XML syntax is awfully cumbersome and is ill suited as the foundation of a general purpose programming language. Consequently, too many conditionals will quickly render your configuration files incomprehensible to subsequent readers, including yourself.

 类似资料: