Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send Email Using Log4j with example
02-06-2011, 08:56 AM
Post: #1
Send Email Using Log4j with example
This article explains how to write a very basic code to email the log statements in the shipped code using Log4j . This does not explain indepth knowledge on writing Log4j Configuration. But, this writeup will be more useful for the beginners who haven't use Log4j Configuration for EMAIL. 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.

Before start using Log4j,lets look into how to install the Log4j , Java Mail API and JavaBeans Activation Framework in your machine.

Install

Download the Apache LOG4j from the link : Download Apache LOG4j
Download the Java Mail API from the link : Download mailapi.jar and smtp.jar
Download the JavaBeans Activation Framework from the link : Download activation.jar
Extract the archived files to some suitable directory into your machine.
Add the file ../log4j-1.2.15.jar to your CLASSPATH environment variable
Add the file mailapi.jar,smtp.jar and activation.jar to your CLASSPATH environment variable
Set up is done. Let's look into our first simple example program using LOG4j.
First,Create a file named SMPTLog4j.properties with the following content.As per the code it should be in the following path

C:\\TestLogger\\etc\\

log4j.logger.com.test.EmailLogger=DEBUG, MAIL
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
#defines how othen emails are send
log4j.appender.MAIL.BufferSize=1
log4j.appender.MAIL.SMTPHost=smpthost
log4j.appender.MAIL.From=contact@developerparadise.com
log4j.appender.MAIL.To=contact@developerparadise.com
log4j.appender.MAIL.Subject=Test Email.
log4j.appender.MAIL.threshold=error
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


In the above propertie file input your SMPThost name.If needed change your FROM,TO and Subject in the SMPTLog4j.properties file.

Create a Java class named EmailLogger .java in com.test package with the following content:

package com.test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class EmailLogger {
private String _log4jProp = null;

private static org.apache.log4j.Logger log = Logger
.getLogger(EmailLogger.class);

public EmailLogger(String pPropertiesFile) {
try {
PropertyConfigurator.configure(pPropertiesFile);
} catch (Exception e) {
System.out.println(e);
}
}

public static void main(String[] args) {
EmailLogger obj = new EmailLogger(
"C:\\TestLogger\\etc\\SMPTLog4j.properties");
log.error("Hello Test Mail");
}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved