Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to send email using log4j

Status
Not open for further replies.

testArav

Programmer
Jan 19, 2005
1
US
Hi,

I want to send an email using Log4j whenever there is an error .

<!-- EMail events to an administrator -->
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
<param name="Threshold" value="ERROR"/>
<param name="To" value="xxx@test.com"/>
<param name="From" value="yyy@test.com"/>
<param name="Subject" value="JBoss Sever Errors"/>
<param name="SMTPHost" value="xxxxx"/>
<param name="BufferSize" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
</layout>
</appender>

I configured the above parameters with appropriate values. Now how do I
send an email when there is an error.I am using jboss server.

Here is the code I am trying to test to send an email..

import org.apache.log4j.*;

public class Log4jExample
{
private static final Logger log =
Logger.getLogger("Log4jExample.class");

public static void main(String[] args)
{
try
{
int i = Integer.parseInt("Hello world");
}
catch (java.lang.Exception ex)
{
java.io.StringWriter strWriter = new java.io.StringWriter();
java.io.PrintWriter writer = new
java.io.PrintWriter(strWriter);
String str = ex.getMessage();
ex.printStackTrace(writer);
log.info(strWriter.toString(), ex);
}
}
}

The above code is not sending an email. Do I need any other settings?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top