• Post Reply Bookmark Topic Watch Topic
  • New Topic

SMTPAppender problem while E-Mailing log file  RSS feed

 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I want email my webapplication log file when ever my log file is populated with ERROR and FATAL level entries for that i am using SMTPAppender but i failed to execute what i insist. Can any body help me out please,

myclasspath containing:: mail.jar (1.2 version), activation.jar

log4j.properties
----------------
# Set root logger level to DEBUG and its only appender to stdout
log4j.rootLogger=INFO, filer ,SMTPTest
log4j.category.org.apache.jk.common.ChannelSocket=DEBUG, filer
# stdout is set to be a RollingFileAppender
log4j.appender.filer=org.apache.log4j.RollingFileAppender
log4j.additivity.org.apache.jk.common.ChannelSocket=false
# stdout uses PatternLayout
log4j.appender.filer.layout=org.apache.log4j.PatternLayout
log4j.appender.filer.layout.ConversionPattern=%d{MMM dd HH:mm:ss} %-5p [%t] %c{2} - %m%n
log4j.appender.filer.File=C:\\COMSProduction.log
log4j.appender.filer.MaxFileSize=512KB
log4j.appender.filer.MaxBackupIndex=4
#SMTPAppender properties
log4j.appender.SMTPTest=org.apache.log4j.net.SMTPAppender
log4j.appender.SMTPTest.Threshold=Error
log4j.appender.SMTPTest.BufferSize=512
log4j.appender.SMTPTest.layout=org.apache.log4j.PatternLayout
log4j.appender.SMTPTest.layout.ConversionPattern=%d %-4r [%t] %-5p (%c:%L)%x - %m%n
log4j.appender.SMTPTest.SMTPHost= mail.dummy.com
log4j.appender.myMail.To=problem@dummy.com
log4j.appender.myMail.From=solution@dummy.com
log4j.appender.SMTPTest.Subject=ProductionLog-Test
log4j.appender.SMTPTest.EvaluatorClass=TriggerAfter1LogEvents

TriggerAfter1LogEvents (My event class)
----------------------
public class TriggerAfter1LogEvents implements TriggeringEventEvaluator {
private int count;

public TriggerAfter1LogEvents() {
count = 0;
}

public boolean isTriggeringEvent(LoggingEvent event) {
if (count >= 1) // <- put your required amount of logging events here
{
count = 0;
return true;
} else {
count++;
return false;
}
}
}

Log4jEmailTest.java (My main class which will send emails)
-------------------
public class Log4jEmailTest {
public static Category CAT = org.apache.log4j.Category.getInstance(Log4jEmailTest.class.getName());

public static void main(String[] args) throws Exception {

Properties properties = null;
try {
properties = new Properties();
InputStream inStream = LogManager.class.getResourceAsStream("log4j.properties");
properties.load(inStream);

CAT.info("Trying to load log4j.properties file..");
PropertyConfigurator.configure(properties);
CAT.info("Successfuly loaded the property file..");
for (int i = 0; i <= 5; i++) {
CAT.error("Wantedly pushing error message into log file based on this error level it will get invoked... " + i);
}
} catch (Exception e) {
CAT.error("OOPS...log4j.property file has not loaded!!");
e.printStackTrace();
}
CAT.debug(" file or fileName is missing");
/**
* Call SMTPAppender and set all required fields to that.
*/
try {
SMTPAppender emailAppender = new SMTPAppender();
emailAppender.setSMTPHost("GGN-HCLT-EVS01.HCLT.CORP.HCL.IN");
emailAppender.setFrom("sirishg@hcl.in");
emailAppender.setTo("ruchirc@hcl.in");
emailAppender.setSubject("COMSProductionLog");
emailAppender.setLayout(new PatternLayout());
emailAppender.activateOptions();
CAT.addAppender(emailAppender);
for (int i = 0; i < 15; i++) {
CAT.error("Test error, error, test, test, fatal error i = " + i);
CAT.fatal("Test error, error, test, test, fatal error i = " + i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

When i run my class i am getting exceptions like,

java.lang.ExceptionInInitializerError
at org.apache.log4j.Category.getInstance(Category.java:514)
at Log4jEmailTest.<clinit>(Log4jEmailTest.java:16)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:87)
Caused by: java.lang.NullPointerException
at javax.mail.internet.AddressParser.tokenizeAddress(AddressParser.java:643)
at javax.mail.internet.AddressParser.parseAddressList(AddressParser.java:79)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:551)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:523)
at org.apache.log4j.net.SMTPAppender.parseAddress(Unknown Source)
at org.apache.log4j.net.SMTPAppender.activateOptions(Unknown Source)
at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:247)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:123)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:87)
at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:645)
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:603)
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:500)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:406)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:432)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:460)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:113)
... 5 more
Exception in thread "main"
Process finished with exit code 1

Any ideas or help appriciable!

Best Regards,
Sirish
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static Category CAT = org.apache.log4j.Category.getInstance(Log4jEmailTest.class.getName());


It's customary to use the class as parameter, not the class name:

org.apache.log4j.Category.getInstance(Log4jEmailTest.class);
 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf!

It's not a problem. but it's not recommended i do agree with you.Finally my code is working fine i have used incompatable jar file like mail.jar and activation.jar now i have upgraded all jar file it's working perfectly.

Thanks for support!

Regards/Sirish
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!