1 package com.upmobile.midccore.commons.logger; 2 3 import java.util.Properties; 4 5 import org.apache.log4j.net.SMTPAppender; 6 import org.apache.log4j.spi.LoggingEvent; 7 import org.springframework.mail.MailException; 8 import org.springframework.mail.SimpleMailMessage; 9 import org.springframework.mail.javamail.JavaMailSenderImpl; 10 11 12 /** 13 * Application builds the SMPT Appender for the Google Mail (gmail). 14 * @author marceloolivas 15 * 16 */ 17 public class GMailAppender extends SMTPAppender { 18 private JavaMailSenderImpl javaMail; 19 private static final String NL = System.getProperty("line.separator"); 20 21 public GMailAppender() { 22 javaMail = new JavaMailSenderImpl(); 23 javaMail.setHost(EmailEnum.HOST.getValue()); 24 javaMail.setPort(Integer.parseInt(EmailEnum.PORT.getValue())); 25 javaMail.setUsername(EmailEnum.USERNAME.getValue()); 26 javaMail.setPassword(EmailEnum.PASSWORD.getValue()); 27 Properties props = new Properties(); 28 props.setProperty(EmailEnum.AUTHORIZED.getValue(), EmailEnum.TRUE.getValue()); 29 props.setProperty(EmailEnum.STARTTTS.getValue(), EmailEnum.TRUE.getValue()); 30 javaMail.setJavaMailProperties(props); 31 32 } 33 34 @Override 35 public void append(LoggingEvent event) { 36 super.append(event); 37 // Create a thread safe "copy" of the template message and customize it 38 SimpleMailMessage msg = new SimpleMailMessage(); 39 StringBuilder builder = new StringBuilder(); 40 builder.append(getLayout().format(event)); 41 builder.append(event.getMessage().toString()); 42 if (event.getThrowableInformation() != null) { 43 builder.append(NL); 44 String[] stackTrace = event.getThrowableInformation().getThrowableStrRep(); 45 for(int i = 0; i < stackTrace.length; i++) { 46 builder.append(stackTrace[i] + NL); 47 } 48 msg.setText(builder.toString()); 49 } 50 String[] senders = getTo().trim().replace(" ", "").split(","); 51 msg.setTo(senders); 52 msg.setSubject(this.getSubject()); 53 try{ 54 javaMail.send(msg); 55 } catch (MailException ex){ 56 } 57 58 } 59 }