Home > JAVA > Send logs by Email Notification using Apache log4j SMTPAppender

Send logs by Email Notification using Apache log4j SMTPAppender

Apache log4j provides log Appender (called SMTPAppender) to send email alerts for the log level configured in log4j configuration file. For example I using simple struts 2 application.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.bluething</groupId>
 <artifactId>SimpleStruts2</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>SimpleStruts2 Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <dependencies>

 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>

 <dependency>
 <groupId>org.apache.struts</groupId>
 <artifactId>struts2-core</artifactId>
 <version>2.3.16.3</version>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
 </dependency>

 <dependency>
 <groupId>javax.mail</groupId>
 <artifactId>mail</artifactId>
 <version>1.4</version>
 </dependency>

 </dependencies>

 <build>
 <finalName>SimpleStruts2</finalName>
 <plugins>
 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.7</source>
 <target>1.7</target>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

struts.xml

</pre>
<pre><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="default" extends="struts-default">
 <action name="getTutorial" class="com.wordpress.itfromzerotohero.action.TutorialAction">
 <result name="success">/success.jsp</result>
 <result name="error">/error.jsp</result>
 </action>
 </package>
</struts>

TutorialAction.java

package com.wordpress.itfromzerotohero.action;

import org.apache.log4j.Logger;

public class TutorialAction {
 private static final Logger logger = Logger.getLogger(TutorialAction.class);
 
 public String execute(){
 System.out.println("Hello from execute()");
 if (logger.isDebugEnabled()) {
 logger.debug("execute()!");
 }
 
 // logs exception
 logger.error("This is Error message", new Exception("Testing"));
 return "success";
 }
 
}

And the important thing is log4j.properties. I’m using mandrilapp service as smtp server.

# Root logger option
log4j.rootLogger=ERROR, stdout, file, MAIL
 
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
 
# Redirect log messages to a log file, support rolling backup file.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/mystruts2app.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.SMTPHost=smtp.mandrillapp.com
log4j.appender.MAIL.SMTPPort=587
log4j.appender.MAIL.SMTPUsername=--fill with smtp username--
log4j.appender.MAIL.SMTPPassword=--fill with smtp password--
log4j.appender.MAIL.From=--fill with mail from--
log4j.appender.MAIL.to=--fill with mail to--
log4j.appender.MAIL.Subject=[Dummy] Error Alert
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n
log4j.appender.MAIL.LevelRangeFilter.LevelMin=error
log4j.appender.MAIL.LevelRangeFilter.LevelMax=fatal

Test it by open this url http://localhost:8080/SimpleStruts2/getTutorial
If worked, an email will be sent.

Categories: JAVA
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: