1 16 17 package org.apache.log4j.net; 18 19 import org.apache.log4j.AppenderSkeleton; 20 import org.apache.log4j.Level; 21 import org.apache.log4j.helpers.CyclicBuffer; 22 import org.apache.log4j.helpers.OptionConverter; 23 import org.apache.log4j.helpers.LogLog; 24 import org.apache.log4j.spi.LoggingEvent; 25 import org.apache.log4j.spi.ErrorCode; 26 import org.apache.log4j.spi.TriggeringEventEvaluator; 27 import java.util.Properties ; 28 import java.util.Date ; 29 30 import javax.mail.Session ; 31 import javax.mail.Transport ; 32 import javax.mail.Message ; 33 import javax.mail.MessagingException ; 34 import javax.mail.internet.MimeMessage ; 35 import javax.mail.Multipart ; 36 import javax.mail.internet.MimeMultipart ; 37 import javax.mail.internet.MimeBodyPart ; 38 import javax.mail.internet.InternetAddress ; 39 import javax.mail.internet.AddressException ; 40 41 54 public class SMTPAppender extends AppenderSkeleton { 55 private String to; 56 private String from; 57 private String subject; 58 private String smtpHost; 59 private int bufferSize = 512; 60 private boolean locationInfo = false; 61 62 protected CyclicBuffer cb = new CyclicBuffer(bufferSize); 63 protected Message msg; 64 65 protected TriggeringEventEvaluator evaluator; 66 67 68 69 73 public 74 SMTPAppender() { 75 this(new DefaultEvaluator()); 76 } 77 78 79 82 public 83 SMTPAppender(TriggeringEventEvaluator evaluator) { 84 this.evaluator = evaluator; 85 } 86 87 88 91 public 92 void activateOptions() { 93 Properties props = new Properties (System.getProperties()); 94 if (smtpHost != null) 95 props.put("mail.smtp.host", smtpHost); 96 97 98 Session session = Session.getInstance(props, null); 99 msg = new MimeMessage (session); 101 102 try { 103 if (from != null) 104 msg.setFrom(getAddress(from)); 105 else 106 msg.setFrom(); 107 108 msg.setRecipients(Message.RecipientType.TO, parseAddress(to)); 109 if(subject != null) 110 msg.setSubject(subject); 111 } catch(MessagingException e) { 112 LogLog.error("Could not activate SMTPAppender options.", e ); 113 } 114 } 115 116 120 public 121 void append(LoggingEvent event) { 122 123 if(!checkEntryConditions()) { 124 return; 125 } 126 127 event.getThreadName(); 128 event.getNDC(); 129 if(locationInfo) { 130 event.getLocationInformation(); 131 } 132 cb.add(event); 133 if(evaluator.isTriggeringEvent(event)) { 134 sendBuffer(); 135 } 136 } 137 138 144 protected 145 boolean checkEntryConditions() { 146 if(this.msg == null) { 147 errorHandler.error("Message object not configured."); 148 return false; 149 } 150 151 if(this.evaluator == null) { 152 errorHandler.error("No TriggeringEventEvaluator is set for appender ["+ 153 name+"]."); 154 return false; 155 } 156 157 158 if(this.layout == null) { 159 errorHandler.error("No layout set for appender named ["+name+"]."); 160 return false; 161 } 162 return true; 163 } 164 165 166 synchronized 167 public 168 void close() { 169 this.closed = true; 170 } 171 172 InternetAddress getAddress(String addressStr) { 173 try { 174 return new InternetAddress (addressStr); 175 } catch(AddressException e) { 176 errorHandler.error("Could not parse address ["+addressStr+"].", e, 177 ErrorCode.ADDRESS_PARSE_FAILURE); 178 return null; 179 } 180 } 181 182 InternetAddress [] parseAddress(String addressStr) { 183 try { 184 return InternetAddress.parse(addressStr, true); 185 } catch(AddressException e) { 186 errorHandler.error("Could not parse address ["+addressStr+"].", e, 187 ErrorCode.ADDRESS_PARSE_FAILURE); 188 return null; 189 } 190 } 191 192 195 public 196 String getTo() { 197 return to; 198 } 199 200 201 204 public 205 boolean requiresLayout() { 206 return true; 207 } 208 209 212 protected 213 void sendBuffer() { 214 215 try { 218 MimeBodyPart part = new MimeBodyPart (); 219 220 StringBuffer sbuf = new StringBuffer (); 221 String t = layout.getHeader(); 222 if(t != null) 223 sbuf.append(t); 224 int len = cb.length(); 225 for(int i = 0; i < len; i++) { 226 LoggingEvent event = cb.get(); 228 sbuf.append(layout.format(event)); 229 if(layout.ignoresThrowable()) { 230 String [] s = event.getThrowableStrRep(); 231 if (s != null) { 232 for(int j = 0; j < s.length; j++) { 233 sbuf.append(s[j]); 234 } 235 } 236 } 237 } 238 t = layout.getFooter(); 239 if(t != null) 240 sbuf.append(t); 241 part.setContent(sbuf.toString(), layout.getContentType()); 242 243 Multipart mp = new MimeMultipart (); 244 mp.addBodyPart(part); 245 msg.setContent(mp); 246 247 msg.setSentDate(new Date ()); 248 Transport.send(msg); 249 } catch(Exception e) { 250 LogLog.error("Error occured while sending e-mail notification.", e); 251 } 252 } 253 254 255 256 259 public 260 String getEvaluatorClass() { 261 return evaluator == null ? null : evaluator.getClass().getName(); 262 } 263 264 267 public 268 String getFrom() { 269 return from; 270 } 271 272 275 public 276 String getSubject() { 277 return subject; 278 } 279 280 284 public 285 void setFrom(String from) { 286 this.from = from; 287 } 288 289 293 public 294 void setSubject(String subject) { 295 this.subject = subject; 296 } 297 298 299 306 public 307 void setBufferSize(int bufferSize) { 308 this.bufferSize = bufferSize; 309 cb.resize(bufferSize); 310 } 311 312 316 public 317 void setSMTPHost(String smtpHost) { 318 this.smtpHost = smtpHost; 319 } 320 321 324 public 325 String getSMTPHost() { 326 return smtpHost; 327 } 328 329 333 public 334 void setTo(String to) { 335 this.to = to; 336 } 337 338 339 340 343 public 344 int getBufferSize() { 345 return bufferSize; 346 } 347 348 355 public 356 void setEvaluatorClass(String value) { 357 evaluator = (TriggeringEventEvaluator) 358 OptionConverter.instantiateByClassName(value, 359 TriggeringEventEvaluator.class, 360 evaluator); 361 } 362 363 364 375 public 376 void setLocationInfo(boolean locationInfo) { 377 this.locationInfo = locationInfo; 378 } 379 380 383 public 384 boolean getLocationInfo() { 385 return locationInfo; 386 } 387 } 388 389 class DefaultEvaluator implements TriggeringEventEvaluator { 390 396 public 397 boolean isTriggeringEvent(LoggingEvent event) { 398 return event.getLevel().isGreaterOrEqual(Level.ERROR); 399 } 400 } 401 | Popular Tags |