admin 管理员组文章数量: 887007
logback.xml if 条件判断
本地开发需要打印控制台日志,而测试以及生产环境则不需要,此时可在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.
本文标签: logbackxml if 条件判断
版权声明:本文标题:logback.xml if 条件判断 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732351145h1533093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论