本地开发需要打印控制台⽇志,⽽测试以及⽣产环境则不需要,此时可在l中通过配置⽂件值进⾏条件判断。
1.先上代码
bis珍惜资源 保护环境
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true"scanPeriod="30 conds">
<springProperty scope="context"name="env"source="v"/>
<appender name="FILE"class="ch.olling.RollingFileAppender">
...
...
</appender>
<appender name="STDOUT"class="ch.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>
my love歌词</if>
</root>
application.properties
<dependency>
<groupId&dehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.6</version>
</dependency>
2.⽂档
Developers often need to juggle between veral logback configuration files targeting different environments such as development, testing and production. The 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 veral environments. Note that conditional processing requires the .
The general format for conditional statements is shown below.
<!-- if-then form -->
<if condition="some conditional expression">
<then>
...
</then>
</if>
<!-- if-then-el form -->
<if condition="some conditional expression">
<then>
...
</then>
herlf
<el>
...
</el>
</if>
The condition is a Java expression in which only context properties or system properties are accessible. For a key pasd 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 wou
makelove什么意思ld 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 ud 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.ConsoleAppender">
<encoder>
<pattern>%d %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="CON"/>
jk罗琳名言
</root>
</then>
lost是什么意思
</if>
<appender name="FILE"class="ch.FileAppender">
<file>${randomOutputDir}/conditional.log</file>
<encoder>
<pattern>%d %-5level %logger{35} - %msg %n</pattern>
moon festival
</encoder>
arturas</appender>
<root level="ERROR">成人学位英语考试成绩查询
<appender-ref ref="FILE"/>
</root>
</configuration>
Conditional processing is supported anywhere within the element. Nested if-then-el statements are also supported.
However, XML syntax is awfully cumbersome and is ill suited as the foundation of a general purpo programming language. Conquently, too many conditionals will quickly render your configuration files incomprehensible to subquent readers, including yourlf.