Logback: Application errors notification

Some months ago when I was doing big application refactoring I found really annoying pieces of log4j based code used for logging repeated hundreds of times:
 
 
 
 
 
 
 
 
 

if (LOG.isDebugEnabled()) {
    LOG.debug("Logging some stuff = " + stuff);
}

I wanted to get rid of isXXXEnabled an thats how I found Logback and SLF4J.  

Logback and SLF4J

Logback in conjunction with SLF4J provides great API and fast and powerful implementation of logging framework. Reasons why to switch to Logback from log4j are not topic of this post and they are already described in details on logback website. Just in short notice what do you get with Logback and SL4FJ:

  • simple and fast way to skip isXXXEnabled with:
     LOG.debug("Logging some stuff = {}", stuff);
  • automatic reloading of configuration files
  • powerful logging filters
  • loading properties file into configuration xml
  • conditional configuration

Thanks to SLF4J migration tool migration from log4j api to SLF4J is super fast. I’ve decided to switch all projects to Logback and after several months I have to say that I am really satisfied with that decision.

In order to use Logback in your project add following dependencies:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.4</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.1</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.4</version>
</dependency>

Setting up error notifications with Logback

In application with proper logging every entry logged with level ERROR should be considered as a problem that should be fixed.

Most common way of logging errors is of course logging exceptions with stacktraces to logfile but if you have plenty of applications on several hosts checking for errors can be time consuming. Smarter way is to log them into database – that can be achieved with DBAppender. I found the most helpful to send all exceptions on my email to be able to fix them immediately.

In order to setup Logback to send mails with exceptions we need to add Java Mail dependency:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

And define configuration for SMTPAppender in logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- some application specific configuration here -->

    <appender name="sheriff" class="ch.qos.logback.classic.net.SMTPAppender">
  <smtpHost>localhost</SMTPHost>
  <from>sheriff@mycompany.com</From>
  <to>john@mycompany.com</To>
  <subject>Something went wrong</Subject>
  <layout class="ch.qos.logback.classic.html.HTMLLayout"/>

  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
   <level>ERROR</level>
  </filter>
 </appender>

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

Thanks to ThresholdFilter with level set to ERROR we are sure that even if root level will be changed – only error mails will be sent.

Now you should get emails that look similar to:

Skipping error notifications in dev environment

Probably you will not need to receive those mails coming from development environment and thanks to conditionals in logback configuration you can easily skip them.

In order to use conditionals you need to add dependency to Janino:

<dependency>
    <groupId>janino</groupId>
    <artifactId>janino</artifactId>
    <version>2.5.10</version>
</dependency>

Next thing is to know whats your environment. In Logback configuration you can access system properties, logback properties or special variables like HOSTNAME and CONTEXT_NAME and them to determine your environment. In my application I use Spring profiles for that so my ROOT logger configuration is:

<root level="ERROR">
    <if condition='"${spring.profiles.active}" == "production"'>
        <appender-ref ref="sheriff" />
    </if>
</root>

Few words for log4j users

If you decide to stick to log4j you can use it’s SMTPAppender.

Log4j can be used together with SLF4J thanks to log4j-over-slf4j library.  

Conclusion

Sending mails with Logback is not the only way to notify about errors. Its possible to use Jabber appender or even write appender that would send errors through SMS gateway. Its up to you.

What solutions for error notifications do you use in your projects?

Reference: Application errors notification with Logback from our JCG partner Maciej Walkowiak at the Software Development Journey blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


+ seven = 12



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close