It's really easy to add error alerting to your java application. If your application logs all errors using log4j and you want these errors emailed out to a support team or to yourself, then all you have to do is add another appender to your log4j.properties file.Let's say you have the following class which logs an error message:
And your log4j properties file is:
Now add a new MAIL appender, so that your properties file looks like this:
Don't forget to place mail.jar and activation.jar on your classpath and then run the application. Check your inbox and voila, you have an alert without making any source code changes!NB: By default, an email message will be sent when an ERROR or higher severity message is appended. If you want to email messages with levels less than ERROR (e.g. INFO) then you currently have to configure your own implementation of the TriggeringEventEvaluator. Setting log4j.appender.MAIL.Threshold=INFO will not work.Further Reading:
SMTPAppender javadoc
SmtpAppender Members and Properties