Conditional logging with Logback in Scala


Hello Folks

In my project , i got a scenario where I wanted conditional logging. I was using Logback framework for the same. I wanted to set different logging level for staging and production.

Either i could do manually changes in logback.xml for logging level for staging and production both. But this is not the good practice. Then I found the solution of implementing the conditions in logback.xml itself and that provided me the better and efficient solution.

First, we will see the way of implementing the conditions in logback.xml. There are 2 ways to do the same :

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>

Now we have learnt how to make conditions. Now we will see what conditional expressions we can use in logback.xml.

There are 3 ways to do this :

1. Using property() or p() :

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.


property("someKey").contains("someValue")

or

p("someKey").contains("someValue")

2. Using isDefined() :

It is used to check whether a property is defined or not.


isDefined("someKey")

3. Using isNull() :

It is used to check whether a property is null or not.


isNull("someKey")

A full Example to make different logging level for staging and production.

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
   <appender name="CON" class="ch.qos.logback.core.ConsoleAppender">
     <encoder>
       <pattern>%d %-5level %logger{35} - %msg %n</pattern>
     </encoder>
   </appender>
   <if condition='p("runMode").contains("prod")'>
     <then>
       <root level="warn">
         <appender-ref ref="CON" />
       </root>
     </then>
     <else>
       <root level="info">
         <appender-ref ref="CON" />
       </root>
     </else>
   </if>
</configuration> 

Set the property as : export runMode=prod

Cheers !!!

Advertisements

About Rishi Khandelwal

Sr. Software Consultant having more than 6 years industry experience. He has working experience in various technologies such as Scala, Java, Play, Akka, Spark, Hive, Cassandra, Akka-http, ElasticSearch, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Agile, Akka, Best Practices, Cloud, Java and tagged , , , , , . Bookmark the permalink.

One Response to Conditional logging with Logback in Scala

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s