Logback: Application errors notification
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.