Lines 1-12
Link Here
|
1 |
/* |
1 |
/* |
2 |
* Copyright 1999,2005 The Apache Software Foundation. |
2 |
* Copyright 1999,2005 The Apache Software Foundation. |
3 |
* |
3 |
* |
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
* you may not use this file except in compliance with the License. |
5 |
* you may not use this file except in compliance with the License. |
6 |
* You may obtain a copy of the License at |
6 |
* You may obtain a copy of the License at |
7 |
* |
7 |
* |
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
9 |
* |
9 |
* |
10 |
* Unless required by applicable law or agreed to in writing, software |
10 |
* Unless required by applicable law or agreed to in writing, software |
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Lines 53-88
Link Here
|
53 |
<code>BufferSize</code> logging events in its cyclic buffer. This |
53 |
<code>BufferSize</code> logging events in its cyclic buffer. This |
54 |
keeps memory requirements at a reasonable level while still |
54 |
keeps memory requirements at a reasonable level while still |
55 |
delivering useful application context. |
55 |
delivering useful application context. |
56 |
|
56 |
|
57 |
<p>There are three ways in which the trigger is fired, resulting in an email |
57 |
<p>There are three ways in which the trigger is fired, resulting in an email |
58 |
containing the buffered events: |
58 |
containing the buffered events: |
59 |
|
59 |
|
60 |
<p>* DEFAULT BEHAVIOR: relies on an internal TriggeringEventEvaluator class that |
60 |
<p>* DEFAULT BEHAVIOR: relies on an internal TriggeringEventEvaluator class that |
61 |
triggers the sending of an email when an event with a severity of ERROR or greater is received. |
61 |
triggers the sending of an email when an event with a severity of ERROR or greater is received. |
62 |
<p>* Set the 'evaluatorClass' param to the fully qualified class name of a class you |
62 |
<p>* Set the 'evaluatorClass' param to the fully qualified class name of a class you |
63 |
have written that implements the TriggeringEventEvaluator interface. |
63 |
have written that implements the TriggeringEventEvaluator interface. |
64 |
<p>* Set the 'expression' param to a valid (infix) expression supported by ExpressionRule and |
64 |
<p>* Set the 'expression' param to a valid (infix) expression supported by ExpressionRule and |
65 |
ExpressionRule's supported operators and operands. |
65 |
ExpressionRule's supported operators and operands. |
66 |
|
66 |
|
67 |
As events are received, events are evaluated against the expression rule. An event |
67 |
As events are received, events are evaluated against the expression rule. An event |
68 |
that causes the rule to evaluate to true triggers the email send. |
68 |
that causes the rule to evaluate to true triggers the email send. |
69 |
|
69 |
|
70 |
If both evaluatorClass and expression params are set, the evaluatorClass is used. |
70 |
If both evaluatorClass and expression params are set, the evaluatorClass is used. |
71 |
|
71 |
|
72 |
See org.apache.log4j.rule.ExpressionRule for a more information. |
72 |
See org.apache.log4j.rule.ExpressionRule for a more information. |
73 |
|
73 |
|
74 |
<p> |
74 |
<p> |
75 |
|
75 |
|
76 |
@author Ceki Gülcü |
76 |
@author Ceki Gülcü |
77 |
@since 1.0 */ |
77 |
@since 1.0 */ |
78 |
public class SMTPAppender extends AppenderSkeleton { |
78 |
public class SMTPAppender extends AppenderSkeleton { |
79 |
private String to; |
79 |
private String to; |
|
|
80 |
/** |
81 |
* Comma separated list of email addresses for the Cc line. |
82 |
*/ |
83 |
private String cc; |
80 |
private String from; |
84 |
private String from; |
81 |
private String subjectStr = ""; |
85 |
private String subjectStr = ""; |
82 |
private String smtpHost; |
86 |
private String smtpHost; |
83 |
private String charset = "ISO-8859-1"; |
87 |
private String charset = "ISO-8859-1"; |
84 |
private int bufferSize = 512; |
88 |
private int bufferSize = 512; |
85 |
private boolean locationInfo = false; |
89 |
private boolean locationInfo = false; |
|
|
90 |
|
91 |
/** |
92 |
* Email header used to set messages to higher or lower priority given a numerical value of 1 through 5. |
93 |
* <ol> |
94 |
* <li>Highest Priority</li> |
95 |
* <li>High Priority</li> |
96 |
* <li>Normal (default if not defined)</li> |
97 |
* <li>Low Priority</li> |
98 |
* <li>Lowest Priority</li> |
99 |
* </ol> |
100 |
*/ |
101 |
private int priority = 3; |
86 |
protected CyclicBuffer cb = new CyclicBuffer(bufferSize); |
102 |
protected CyclicBuffer cb = new CyclicBuffer(bufferSize); |
87 |
protected MimeMessage msg; |
103 |
protected MimeMessage msg; |
88 |
protected TriggeringEventEvaluator evaluator; |
104 |
protected TriggeringEventEvaluator evaluator; |
Lines 128-133
Link Here
|
128 |
} |
144 |
} |
129 |
|
145 |
|
130 |
msg.setRecipients(Message.RecipientType.TO, parseAddress(to)); |
146 |
msg.setRecipients(Message.RecipientType.TO, parseAddress(to)); |
|
|
147 |
|
148 |
//Add CC receipients if defined. |
149 |
if (cc != null && !"".equals(cc)) { |
150 |
msg.setRecipients(Message.RecipientType.CC, parseAddress(cc)); |
151 |
} |
152 |
|
153 |
//Set email priority |
154 |
msg.addHeader("X-Priority", String.valueOf(getPriority())); |
155 |
|
131 |
} catch (MessagingException e) { |
156 |
} catch (MessagingException e) { |
132 |
errorCount++; |
157 |
errorCount++; |
133 |
getLogger().error("Could not activate SMTPAppender options.", e); |
158 |
getLogger().error("Could not activate SMTPAppender options.", e); |
Lines 153-159
Link Here
|
153 |
getLogger().error(errMsg); |
178 |
getLogger().error(errMsg); |
154 |
throw new IllegalStateException(errMsg); |
179 |
throw new IllegalStateException(errMsg); |
155 |
} |
180 |
} |
156 |
|
181 |
|
157 |
if(errorCount == 0) { |
182 |
if(errorCount == 0) { |
158 |
super.activateOptions(); |
183 |
super.activateOptions(); |
159 |
} |
184 |
} |
Lines 252-261
Link Here
|
252 |
// appender. This frees us from needing to synchronize on 'cb'. |
277 |
// appender. This frees us from needing to synchronize on 'cb'. |
253 |
try { |
278 |
try { |
254 |
MimeBodyPart part = new MimeBodyPart(); |
279 |
MimeBodyPart part = new MimeBodyPart(); |
255 |
|
280 |
|
256 |
String computedSubject = computeSubject(triggeringEvent); |
281 |
String computedSubject = computeSubject(triggeringEvent); |
257 |
msg.setSubject(computedSubject, charset); |
282 |
msg.setSubject(computedSubject, charset); |
258 |
|
283 |
|
259 |
StringBuffer sbuf = new StringBuffer(); |
284 |
StringBuffer sbuf = new StringBuffer(); |
260 |
String t = layout.getHeader(); |
285 |
String t = layout.getHeader(); |
261 |
|
286 |
|
Lines 321-327
Link Here
|
321 |
public String getFrom() { |
346 |
public String getFrom() { |
322 |
return from; |
347 |
return from; |
323 |
} |
348 |
} |
324 |
|
349 |
|
325 |
/** |
350 |
/** |
326 |
Returns value of the <b>Subject</b> option. |
351 |
Returns value of the <b>Subject</b> option. |
327 |
*/ |
352 |
*/ |
Lines 338-352
Link Here
|
338 |
} |
363 |
} |
339 |
|
364 |
|
340 |
/** |
365 |
/** |
341 |
* The <b>Subject</b> option takes a string value which will be the subject |
366 |
* The <b>Subject</b> option takes a string value which will be the subject |
342 |
* of the e-mail message. This value can be string literal or a conversion |
367 |
* of the e-mail message. This value can be string literal or a conversion |
343 |
* pattern in the same format as expected by |
368 |
* pattern in the same format as expected by |
344 |
* {@link org.apache.log4j.PatternLayout}. |
369 |
* {@link org.apache.log4j.PatternLayout}. |
345 |
* |
370 |
* |
346 |
* <p>The conversion pattern is applied on the triggering event to dynamically |
371 |
* <p>The conversion pattern is applied on the triggering event to dynamically |
347 |
* compute the subject of the outging email message. For example, setting |
372 |
* compute the subject of the outging email message. For example, setting |
348 |
* the <b>Subject</b> option to "%properties{host} - %m" |
373 |
* the <b>Subject</b> option to "%properties{host} - %m" |
349 |
* will set the subject of outgoing message to the "host" property of the |
374 |
* will set the subject of outgoing message to the "host" property of the |
350 |
* triggering event followed by the message of the triggering event. |
375 |
* triggering event followed by the message of the triggering event. |
351 |
*/ |
376 |
*/ |
352 |
public void setSubject(String subject) { |
377 |
public void setSubject(String subject) { |
Lines 388-393
Link Here
|
388 |
this.to = to; |
413 |
this.to = to; |
389 |
} |
414 |
} |
390 |
|
415 |
|
|
|
416 |
/** |
417 |
The <b>Cc</b> option takes a string value which should be a |
418 |
comma separated list of e-mail address of the recipients. |
419 |
*/ |
420 |
public void setCc(String cc) { |
421 |
this.cc = cc; |
422 |
} |
423 |
|
424 |
/** |
425 |
Returns value of the <b>Cc</b> option. |
426 |
*/ |
427 |
public String getCc() { |
428 |
return cc; |
429 |
} |
430 |
|
431 |
/** |
432 |
The <b>Priority</b> option takes a positive integer (1 through 5) |
433 |
representing the priority of the email message. Default is 3 (aka normal). |
434 |
*/ |
435 |
public void setPriority(int priority) { |
436 |
this.priority = priority; |
437 |
} |
438 |
|
439 |
/** |
440 |
Returns value of the <b>Priority</b> option. |
441 |
*/ |
442 |
public int getPriority() { |
443 |
return priority; |
444 |
} |
445 |
|
391 |
/** |
446 |
/** |
392 |
Returns value of the <b>BufferSize</b> option. |
447 |
Returns value of the <b>BufferSize</b> option. |
393 |
*/ |
448 |
*/ |
Lines 396-406
Link Here
|
396 |
} |
451 |
} |
397 |
|
452 |
|
398 |
/** |
453 |
/** |
399 |
* The <b>EvaluatorClass</b> option takes a string value representing the |
454 |
* The <b>EvaluatorClass</b> option takes a string value representing the |
400 |
* name of the class implementing the {@link TriggeringEventEvaluator} |
455 |
* name of the class implementing the {@link TriggeringEventEvaluator} |
401 |
* interface. A corresponding object will be instantiated and assigned as |
456 |
* interface. A corresponding object will be instantiated and assigned as |
402 |
* the triggering event evaluator for the SMTPAppender. |
457 |
* the triggering event evaluator for the SMTPAppender. |
403 |
* |
458 |
* |
404 |
* @deprecated replaced by {@link #setEvaluator}. |
459 |
* @deprecated replaced by {@link #setEvaluator}. |
405 |
*/ |
460 |
*/ |
406 |
public void setEvaluatorClass(String value) { |
461 |
public void setEvaluatorClass(String value) { |
Lines 465-477
Link Here
|
465 |
|
520 |
|
466 |
private Rule expressionRule; |
521 |
private Rule expressionRule; |
467 |
private String expression; |
522 |
private String expression; |
468 |
|
523 |
|
469 |
public DefaultEvaluator() {} |
524 |
public DefaultEvaluator() {} |
470 |
|
525 |
|
471 |
public void setExpression(String expression) { |
526 |
public void setExpression(String expression) { |
472 |
this.expression = expression; |
527 |
this.expression = expression; |
473 |
} |
528 |
} |
474 |
|
529 |
|
475 |
public void activateOptions() { |
530 |
public void activateOptions() { |
476 |
if(expression != null) { |
531 |
if(expression != null) { |
477 |
try { |
532 |
try { |
Lines 481-487
Link Here
|
481 |
} |
536 |
} |
482 |
} |
537 |
} |
483 |
} |
538 |
} |
484 |
|
539 |
|
485 |
/** |
540 |
/** |
486 |
Is this <code>event</code> the e-mail triggering event? |
541 |
Is this <code>event</code> the e-mail triggering event? |
487 |
|
542 |
|