In today’s post I’m going to show you how to filter log statements into a warning email. This came out of a necessity to monitor a few critical points of one application I was working on. There are tools that you can use to perform application monitoring. I’m not going into details about those tools, but sometimes it’s just easier to have the application send a warning email.
I mostly use log4j for my logging requirements. Unfortunately, since there are so many logging frameworks in the Java ecosystem this post only covers a piece of it. I might do something for the others in the future, but I would like to reinforce a old post of António Gonçalves about standardize a logging API: I need you for Logging API Spec Lead !. The sample covered here is for log4j, but the github project also contains a log4j2 sample.
Use Case
To give a little more detail, I want to be informed when the application generates erros, but also ignore erros that are already handled by the application itself. For a more concrete example, I had a case where a database insert could generate a constraint violation exception, but this error was handled specifically by the application. Even so, the JDBC driver logs the exception. For this case, I was not interested in getting a notification.
Setting up SMTPAppender
Anyway, looking into log4j, you can create an appender that sends all your log to the email, just check SMTPAppender. It looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <appender name="SMTP" class="org.apache.log4j.net.SMTPAppender"> <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/> <param name="Threshold" value="ERROR"/> <param name="To" value="someone@somemail.com"/> <param name="From" value="someonelse@somemail.com"/> <param name="Subject" value="Log Errors"/> <param name="SMTPHost" value="smtp.somemail.com"/> <param name="SMTPUsername" value="username"/> <param name="SMTPPassword" value="password"/> <param name="BufferSize" value="1"/> <param name="SMTPDebug" value="true"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n"/> </layout> </appender> |
Filtering
Our filtering needs are not available in the standard log4j lib. You need to use the log4j-extras which provide you with ExpressionFilter that supports filtering of complex expressions. We are also using StringMatchFilter from the regular log4j lib.
Now, we can add a triggeringPolicy
to the SMTPAppender:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <triggeringPolicy class="org.apache.log4j.rolling.FilterBasedTriggeringPolicy"> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="ERROR01"/> <param name="AcceptOnMatch" value="false"/> </filter> <filter class="org.apache.log4j.filter.ExpressionFilter"> <param name="expression" value="CLASS LIKE .*Log4jExpressionFilter.*"/> <param name="acceptOnMatch" value="false"/> </filter> <filter class="org.apache.log4j.filter.LevelRangeFilter"> <param name="levelMin" value="ERROR"/> <param name="levelMax" value="FATAL"/> </filter> </triggeringPolicy> |
This configuration will filter the log to email only the ERROR
and FATAL
thresholds which are NOT logged in classes with Log4jExpressionFilter
in it’s name and DON’T have ERROR01
in the log message.
Have a look into LoggingEventFieldResolver to see which others expressions you can use with ExpressionFilter. You can use EXCEPTION, METHOD and a few others which are very useful.
Testing
Testing a SMTPAppender is not easy if you rely on real servers. Fortunately, you can use mock-javamail and you don’t even have to worry about polluting a SMTP server. This is also included in the github project.
Resources
You can clone a full working copy from my github repository for log4j and log4j2.
Log4j Mail Filter
Since I may modify the code in the future, you can download the original source of this post from the release 1.0. In alternative, clone the repo, and checkout the tag from release 1.0 with the following command: git checkout 1.0
.
I have been a long time user (and customer) of IntelliJ IDEA. I think I have started using it around 2005 or 2006, version 5.0 at the time. I was an Eclipse user back then. A few of my colleagues recommended it to me, and at first I was not convinced, but after trying it out I was impressed.
Now in 2014, IntelliJ IDEA is still my IDE of choice. The intent of this post is not to start an IDE war, but to focus on a few of IDEA features that sometimes other IDEA users are not aware of.
Darcula Theme
The Darcula Theme changes your user interface to a dark look and feel. Well, maybe this is nothing new for you, but I would like to point two major advantages. First, it causes much less stress to your eyes. Give it a try! After a few hours using the dark look if you switch to the default one again you’re probably going to feel your eyes burning for a few minutes. Second, if you’re a mobility addict and you’re always running on battery, the dark look can also help your battery to last longer.

Postfix completion
Postfix completion is the feature that I always wanted and I didn’t even know it. Postfix completion allows you to change already typed expressions. How many times all of us have cursed for having to return back to add a missing cast? Or because we actually wanted to System.out the expression? Well, Postfix completion fixes that.
For instance for the System.out, you type the expression:
someVar
You can now type:
someVar.sout
And the expression is transformed to:
System.out.println(someVar);
Check this awesome post in IntelliJ Blog for additional information about Postfix completion.
Frameworks and Technologies Support
In the Java world, you have a lot of frameworks and technologies available. Most likely you will come across to many of them in your developer work. Sometimes, it’s a nightmare to deal with the additional layer and the required configuration for everything to work correctly.
Look at Maven for instance, it’s a pain to find which dependency to import when you need a class. IDEA Maven support, allows you to search for the class in your local repository and add the correct dependency to your pom.xml file.
Just type the name of the class, press Alt + Enter and Add Maven Dependency:

Pick the library you need. It’s added automatically to your pom.xml.

You have support for Java EE, Spring, GWT, Maven and many others. Check here for a full list.
Inject Language
With Inject Language, it’s possible to have syntax, error highlighting and code completion for a large number of languages into String literals. I use GWT a lot, and this allows me to be able to write safe HTML into the String HTML parameters of the API, like this:

Other examples include, SQL, CSS, Javascript, Groovy, Scala and many others. Try it out by yourself by pressing Alt + Enter on a String statement and then Inject Language.
Presentation Mode
Did you ever had the need to make a presentation about code using your IDE and the audience is not able to see it properly due to font size? And then you have to interrupt your presentation to adjust it. Sometimes you don’t even remember where to adjust it. Wouldn’t be easier to just have a dedicate presentation mode? Just go to View menu and then Enter Presentation Mode option.
Conclusion
I do believe that choosing an IDE is a matter of personal preference and you should stick with the one you feel more productive for the task that you have to complete. I still use Eclipse when I have to deal with BPM stuff.
Some of these features also exist on the other IDE’s, but I have the impression by chatting with other developers that they don’t know about their existence. Explore your development environment and I’m pretty sure you will learn something new. I’m always learning new stuff in IntelliJ IDEA.
Missing a feature you love? Feel free to post it in the comments section!