Home » Java » Enterprise Java » Logback: Application errors notification

About Maciej Walkowiak

Maciej Walkowiak

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.

(+1 rating, 1 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
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 our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments