[logback-dev] if-then-else support in Joran.

Ceki Gülcü ceki at qos.ch
Mon Mar 29 23:07:10 CEST 2010


Just committed new code so that the following configuration file is
interpreted correctly.

<configuration>

   <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>


   <if condition='property("HOSTNAME").contains("${aHost}")'>
     <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>

</configuration>

where "aHost" is set to be the name of the current host. Note that
HOSTNAME always contains the name of the current host so that the
condition property("HOSTNAME").contains("${aHost}") evaluates to true.

In my previous message, the config file get set the incremental
attribute of the root element to true. No such attribute
exists. Logger declaration in logback configuration files are
incremental by design. I mistakenly thought that it was like in log4j
where each logger declaration removes all attached appenders.

I hope that you'll find the XML-conditional syntax not too
horrible. Your comments are welcome,

--
Ceki


On 29/03/2010 9:50 PM, Ceki Gülcü wrote:
>
> Hello all,
>
> I began implementing if-then-else support in Joran. If you wish to
> enable part of a config file in one environment and disable it
> another, now you can.
>
> Here is an example,
>
> <configuration>
>
> <appender name="FILE" class="ch.qos.logback.core.FileAppender">
> <encoder>
> <pattern>%d %-5level %logger{35} - %msg %n</pattern>
> </encoder>
> </appender>
>
> <root level="DEBUG">
> <appender-ref ref="FILE" />
> </root>
>
>
> <if condition='property("HOSTNAME").contains("mydevHost")'>
> <then>
> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
> <encoder>
> <pattern>%d %-5level %logger{35} - %msg %n</pattern>
> </encoder>
> </appender>
> <root incremental="true">
> <appender-ref ref="STDOUT" />
> </root>
> </then>
> </if>
>
> </configuration>
>
> Thus, the ConsoleAppender will be attached to the root logger only on
> "mydevHost" but not on other hosts.
>
> This feature is still experimental. Your feedback is welcome.
>
> --
> Ceki
>
> _______________________________________________
> logback-dev mailing list
> logback-dev at qos.ch
> http://qos.ch/mailman/listinfo/logback-dev



More information about the logback-dev mailing list