📄 smtpappender.java
字号:
sbuf.append(t); int len = cb.length(); for(int i = 0; i < len; i++) { //sbuf.append(MimeUtility.encodeText(layout.format(cb.get()))); LoggingEvent event = cb.get(); sbuf.append(layout.format(event)); if(layout.ignoresThrowable()) { String[] s = event.getThrowableStrRep(); if (s != null) { for(int j = 0; j < s.length; j++) { sbuf.append(s[j]); sbuf.append(Layout.LINE_SEP); } } } } t = layout.getFooter(); if(t != null) sbuf.append(t); part.setContent(sbuf.toString(), layout.getContentType()); Multipart mp = new MimeMultipart(); mp.addBodyPart(part); msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg); } catch(Exception e) { LogLog.error("Error occured while sending e-mail notification.", e); } } /** Returns value of the <b>EvaluatorClass</b> option. */ public String getEvaluatorClass() { return evaluator == null ? null : evaluator.getClass().getName(); } /** Returns value of the <b>From</b> option. */ public String getFrom() { return from; } /** Returns value of the <b>Subject</b> option. */ public String getSubject() { return subject; } /** The <b>From</b> option takes a string value which should be a e-mail address of the sender. */ public void setFrom(String from) { this.from = from; } /** The <b>Subject</b> option takes a string value which should be a the subject of the e-mail message. */ public void setSubject(String subject) { this.subject = subject; } /** The <b>BufferSize</b> option takes a positive integer representing the maximum number of logging events to collect in a cyclic buffer. When the <code>BufferSize</code> is reached, oldest events are deleted as new events are added to the buffer. By default the size of the cyclic buffer is 512 events. */ public void setBufferSize(int bufferSize) { this.bufferSize = bufferSize; cb.resize(bufferSize); } /** The <b>SMTPHost</b> option takes a string value which should be a the host name of the SMTP server that will send the e-mail message. */ public void setSMTPHost(String smtpHost) { this.smtpHost = smtpHost; } /** Returns value of the <b>SMTPHost</b> option. */ public String getSMTPHost() { return smtpHost; } /** The <b>To</b> option takes a string value which should be a comma separated list of e-mail address of the recipients. */ public void setTo(String to) { this.to = to; } /** Returns value of the <b>BufferSize</b> option. */ public int getBufferSize() { return bufferSize; } /** The <b>EvaluatorClass</b> option takes a string value representing the name of the class implementing the {@link TriggeringEventEvaluator} interface. A corresponding object will be instantiated and assigned as the triggering event evaluator for the SMTPAppender. */ public void setEvaluatorClass(String value) { evaluator = (TriggeringEventEvaluator) OptionConverter.instantiateByClassName(value, TriggeringEventEvaluator.class, evaluator); } /** The <b>LocationInfo</b> option takes a boolean value. By default, it is set to false which means there will be no effort to extract the location information related to the event. As a result, the layout that formats the events as they are sent out in an e-mail is likely to place the wrong location information (if present in the format). <p>Location information extraction is comparatively very slow and should be avoided unless performance is not a concern. */ public void setLocationInfo(boolean locationInfo) { this.locationInfo = locationInfo; } /** Returns value of the <b>LocationInfo</b> option. */ public boolean getLocationInfo() { return locationInfo; } /** Set the cc recipient addresses. @param addresses recipient addresses as comma separated string, may be null. */ public void setCc(final String addresses) { this.cc = addresses; } /** Get the cc recipient addresses. @return recipient addresses as comma separated string, may be null. */ public String getCc() { return cc; } /** Set the bcc recipient addresses. @param addresses recipient addresses as comma separated string, may be null. */ public void setBcc(final String addresses) { this.bcc = addresses; } /** Get the bcc recipient addresses. @return recipient addresses as comma separated string, may be null. */ public String getBcc() { return bcc; } /** * The <b>SmtpPassword</b> option takes a string value which should be the password required to authenticate against * the mail server. * @param password password, may be null. */ public void setSMTPPassword(final String password) { this.smtpPassword = password; } /** * The <b>SmtpUsername</b> option takes a string value which should be the username required to authenticate against * the mail server. * @param username user name, may be null. */ public void setSMTPUsername(final String username) { this.smtpUsername = username; } /** * Setting the <b>SmtpDebug</b> option to true will cause the mail session to log its server interaction to stdout. * This can be useful when debuging the appender but should not be used during production because username and * password information is included in the output. * @param debug debug flag. */ public void setSMTPDebug(final boolean debug) { this.smtpDebug = debug; } /** * Get SMTP password. * @return SMTP password, may be null. */ public String getSMTPPassword() { return smtpPassword; } /** * Get SMTP user name. * @return SMTP user name, may be null. */ public String getSMTPUsername() { return smtpUsername; } /** * Get SMTP debug. * @return SMTP debug flag. */ public boolean getSMTPDebug() { return smtpDebug; } /** * Sets triggering evaluator. * @param trigger triggering event evaluator. * @since 1.2.15 */ public final void setEvaluator(final TriggeringEventEvaluator trigger) { if (trigger == null) { throw new NullPointerException("trigger"); } this.evaluator = trigger; } /** * Get triggering evaluator. * @return triggering event evaluator. * @since 1.2.15 */ public final TriggeringEventEvaluator getEvaluator() { return evaluator; } /** {@inheritDoc} */ public boolean parseUnrecognizedElement(final Element element, final Properties props) throws Exception { if ("triggeringPolicy".equals(element.getNodeName())) { Object triggerPolicy = org.apache.log4j.xml.DOMConfigurator.parseElement( element, props, TriggeringEventEvaluator.class); if (triggerPolicy instanceof TriggeringEventEvaluator) { setEvaluator((TriggeringEventEvaluator) triggerPolicy); } return true; } return false; }}class DefaultEvaluator implements TriggeringEventEvaluator { /** Is this <code>event</code> the e-mail triggering event? <p>This method returns <code>true</code>, if the event level has ERROR level or higher. Otherwise it returns <code>false</code>. */ public boolean isTriggeringEvent(LoggingEvent event) { return event.getLevel().isGreaterOrEqual(Level.ERROR); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -