Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using log4j to send email reports via the SMTPAppender

I'm trying to use log4j to send emailable reports that contain the logging statements from a background process. I want one email sent for each process run, not one email for each logging statement. I've looked at the SMTPAppender, but don't see a way to manually send the report when the process completes. I believe the TriggeringEventEvaluator may be the key, but one issue I'm running into is how to get a handle to the TriggeringEventEvaluator instance. I'm stuck using log4j 1.2.14 and the SMTPAppender.getEvaluator() method was introduced in 1.2.15. Any thoughts? Am I even on the right track? Does the SMTPAppender.close() method come into play here?

I want to be able to do this:

log.info(message1);
log.info(message2);
log.info(message3);
log.sendMail();

After thinking about this some more, I think I need to clarify what I'm hoping to accomplish. I'm trying to capture the logging from running a quartz job and send the resulting log as an email. The quartz job makes a bunch of service method calls into various services. I want the to include any logging those service methods perform as well as the logging of the quartz jobs itself. I was thinking I could do something like the following for capturing all the logging, but it isn't working.

// at the beginning of quartz job
Logger logger = Logger.getRootLogger();
StringWriter sw = new StringWriter();
WriterAppender wa = new WriterAppender(new SimpleLayout(), sw);
logger.addAppender(wa);

// at the end of the quartz job 
String report = sw.toString();
like image 684
fmpdmb Avatar asked Nov 29 '10 17:11

fmpdmb


People also ask

What is SMTP Appender?

The SMTPAppender keeps only the last BufferSize logging events in its cyclic buffer. This keeps memory requirements at a reasonable level while still delivering useful application context. By default, an email message will be sent when an ERROR or higher severity message is appended.

How does log4j logging work?

Log4j allows logged messages to contain format strings that reference external information through the Java Naming and Directory Interface (JNDI). This allows information to be remotely retrieved across a variety of protocols, including the Lightweight Directory Access Protocol (LDAP).

Which log4j component is responsible for logging messages?

log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.

Does Papertrail use log4j?

Papertrail supports aggregating messages from a native log4j 1. x or log4j 2 appender, providing a live searchable console for your Java (JRE/JVM) app logs.


1 Answers

You shouldn't use any of log4j's methods, you should configure it properly instead.

First of all, define in your log4j.properties file your appender properly:

#CONFIGURE SMTP
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=mail.mydomain.com
[email protected]
log4j.appender.email.SMTPPassword=mypw
[email protected]
[email protected]
log4j.appender.email.Subject=Log of messages
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m

Note: code taken from this post. More information can be obtained in SMTPAppender API.

Next, make a special class that will be used just for sending email. Example:

package com.foo.mailer;
import org.apache.log4j.Logger;

public class Mailer {
   private static final Logger logger = Logger.getLogger(Mailer.class);

   public void logMail(String mailString) {
      logger.info(mailString);
   }
}

Next, put in log4j.properties configuration for this class:

# INFO level will be logged
log4j.logger.com.foo.mailer = INFO, email
# turn off additivity
log4j.additivity.com.foo.mailer = false

Now, whenever you want to send an email using log4j, put this in your code:

new Mailer().logMail("This mail should be sent");

Disclaimer: I haven't tested any of this code.

like image 123
darioo Avatar answered Oct 03 '22 15:10

darioo