E-Mail reporting

This commit is contained in:
2012-10-07 18:38:57 +02:00
parent 3351daaef4
commit 8b4eac33fc
9 changed files with 244 additions and 86 deletions
@@ -0,0 +1,98 @@
package de.tilman.log4j;
import java.util.LinkedList;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
public class EmailCollector extends AppenderSkeleton {
private final static Logger log = Logger.getLogger(EmailCollector.class);
/**
* Maximum number of lines to collect
*/
private int bufferSize;
private LinkedList<String> lineBuffer;
private String smtpUser;
private String smtpPassword;
private String emailRecipient;
private String emailSubject;
public EmailCollector(int bufferSize, String smtpUser, String smtpPassword, String emailRecipient, String emailSubject) {
this.bufferSize = bufferSize;
lineBuffer = new LinkedList<String>();
this.smtpUser= smtpUser;
this.smtpPassword = smtpPassword;
this.emailRecipient = emailRecipient;
this.emailSubject = emailSubject;
}
/**
* Close the EmailCollector and send collected messages.
*
* @see org.apache.log4j.Appender#close()
*/
@Override
public void close() {
log.info("Sending e-mail report to " + emailRecipient);
String host = "smtp.gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", smtpUser);
props.put("mail.smtp.password", smtpPassword);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(smtpUser));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailRecipient));
message.setSubject(emailSubject);
StringBuffer sb = new StringBuffer();
while (lineBuffer.size() > 0) {
sb.append(lineBuffer.poll());
sb.append("\n");
}
message.setText(sb.toString());
Transport transport = session.getTransport("smtp");
transport.connect(host, smtpUser, smtpPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
log.error("Error sending e-mail", e);
}
this.closed = true;
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
protected void append(LoggingEvent event) {
if (lineBuffer.size() >= bufferSize) {
lineBuffer.removeFirst();
}
lineBuffer.add(event.toString());
}
}
@@ -34,7 +34,7 @@ import org.jivesoftware.smack.packet.Message;
/**
* A simple log4j appender that connects the logger to an XMPP recipient.
*
* @author Tilman Walther
* @author Tilman Liero
*/
public class JabberAppender extends AppenderSkeleton {
+32 -4
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Tilman Walther
* Copyright 2011, 2012 Tilman Liero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
@@ -50,13 +51,14 @@ import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;
import com.martiansoftware.jsap.UnflaggedOption;
import de.tilman.log4j.EmailCollector;
import de.tilman.log4j.JabberAppender;
/**
* SyncTool synchronized two directories recursively.
*
* @author Tilman Walther
* @author Tilman Liero
*/
public class SyncTool {
@@ -449,10 +451,13 @@ public class SyncTool {
}
/**
* @param args
*/
public static void main(String[] args) {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n")));
log.info("Starting SyncTool version 1.2.1");
log.info("Starting SyncTool version 1.3");
JSAP jsap = new JSAP();
@@ -477,6 +482,21 @@ public class SyncTool {
logfileOption.setHelp("the path for a logfile to write");
jsap.registerParameter(logfileOption);
FlaggedOption smtpUser = new FlaggedOption("SMTP user").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"smtpuser").setShortFlag('t');
smtpUser.setHelp("the SMTP user for e-mail reporting");
jsap.registerParameter(smtpUser);
FlaggedOption smtpPassword = new FlaggedOption("SMTP password").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"smtppassword").setShortFlag('w');
smtpPassword.setHelp("the SMTP password used for sending the e-mail report");
jsap.registerParameter(smtpPassword);
FlaggedOption emailAddress = new FlaggedOption("e-mail address").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"email").setShortFlag('e');
emailAddress.setHelp("send logging output as jabber message to the given address");
jsap.registerParameter(emailAddress);
FlaggedOption jabberAddress = new FlaggedOption("jabber address").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"jabber").setShortFlag('j');
jabberAddress.setHelp("send logging output as jabber message to the given address");
@@ -519,7 +539,7 @@ public class SyncTool {
FlaggedOption checkFileExists = new FlaggedOption("checkfile").setStringParser(JSAP.STRING_PARSER).setLongFlag(
"check-file-exists");
checkFileExists.setHelp("perfom synchronization only if the given file exists");
checkFileExists.setHelp("perform synchronization only if the given file exists");
jsap.registerParameter(checkFileExists);
Switch debugSwitch = new Switch("debug").setLongFlag("debug");
@@ -570,6 +590,11 @@ public class SyncTool {
}
}
if (config.getString("SMTP user") != null) {
BasicConfigurator.configure(new EmailCollector(200, config.getString("smtpuser"), config.getString("smtppassword"), config.getString("email"), "SyncTool Report"));
log.info("Prepared e-mail report for " + config.getString("e-mail address"));
}
if (config.getString("jabber address") != null) {
try {
BasicConfigurator.configure(new JabberAppender(config.getString("jabber address"), config.getString("jabber server"), config.getString("jabber user"), config.getString("jabber password")));
@@ -589,5 +614,8 @@ public class SyncTool {
SyncTool syncTool = new SyncTool(config);
syncTool.sync(config.getString("source path"), config.getString("destination path"));
// shutting down the logger will trigger the generation of the e-mail report
LogManager.shutdown();
}
}