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:




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:


And define configuration for SMTPAppender in logback.xml:

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

    <appender name="sheriff" class="ch.qos.logback.classic.net.SMTPAppender">
  <subject>Something went wrong</Subject>
  <layout class="ch.qos.logback.classic.html.HTMLLayout"/>

  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

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

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:


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

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.  


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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

− one = 8

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books