View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.core.appender;
19  
20  import org.apache.logging.log4j.core.config.plugins.Plugin;
21  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
22  import org.apache.logging.log4j.core.config.plugins.PluginElement;
23  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
24  import org.apache.logging.log4j.core.filter.ThresholdFilter;
25  import org.apache.logging.log4j.core.layout.HTMLLayout;
26  import org.apache.logging.log4j.core.net.SMTPManager;
27  import org.apache.logging.log4j.core.Filter;
28  import org.apache.logging.log4j.core.Layout;
29  import org.apache.logging.log4j.core.LogEvent;
30  
31  /**
32   * Send an e-mail when a specific logging event occurs, typically on errors or
33   * fatal errors.
34   *
35   * <p>
36   * The number of logging events delivered in this e-mail depend on the value of
37   * <b>BufferSize</b> option. The <code>SMTPAppender</code> keeps only the last
38   * <code>BufferSize</code> logging events in its cyclic buffer. This keeps
39   * memory requirements at a reasonable level while still delivering useful
40   * application context.
41   *
42   * By default, an email message will formatted as HTML. This can be modified by
43   * setting a layout for the appender.
44   *
45   * By default, an email message will be sent when an ERROR or higher severity
46   * message is appended. This can be modified by setting a filter for the
47   * appender.
48   */
49  @Plugin(name = "SMTP", type = "Core", elementType = "appender", printObject = true)
50  public final class SMTPAppender extends AbstractAppender {
51  
52      private static final int DEFAULT_BUFFER_SIZE = 512;
53  
54      /** The SMTP Manager */
55      protected final SMTPManager manager;
56  
57      private SMTPAppender(final String name, final Filter filter, final Layout<?> layout, final SMTPManager manager,
58                           final boolean handleExceptions) {
59          super(name, filter, layout, handleExceptions);
60          this.manager = manager;
61      }
62  
63      /**
64       * Create a SMTPAppender.
65       *
66       * @param name
67       *            The name of the Appender.
68       * @param to
69       *            The comma-separated list of recipient email addresses.
70       * @param cc
71       *            The comma-separated list of CC email addresses.
72       * @param bcc
73       *            The comma-separated list of BCC email addresses.
74       * @param from
75       *            The email address of the sender.
76       * @param replyTo
77       *            The comma-separated list of reply-to email addresses.
78       * @param subject The subject of the email message.
79       * @param smtpProtocol The SMTP transport protocol (such as "smtps", defaults to "smtp").
80       * @param smtpHost
81       *            The SMTP hostname to send to.
82       * @param smtpPortNum
83       *            The SMTP port to send to.
84       * @param smtpUsername
85       *            The username required to authenticate against the SMTP server.
86       * @param smtpPassword
87       *            The password required to authenticate against the SMTP server.
88       * @param smtpDebug
89       *            Enable mail session debuging on STDOUT.
90       * @param bufferSizeNum
91       *            How many log events should be buffered for inclusion in the
92       *            message?
93       * @param layout
94       *            The layout to use (defaults to HTMLLayout).
95       * @param filter
96       *            The Filter or null (defaults to ThresholdFilter, level of
97       *            ERROR).
98       * @param suppressExceptions
99       *            "true" if exceptions should be hidden from the application,
100      *            "false" otherwise (defaults to "true").
101      * @return The SMTPAppender.
102      */
103     @PluginFactory
104     public static SMTPAppender createAppender(@PluginAttr("name") final String name,
105                                               @PluginAttr("to") final String to,
106                                               @PluginAttr("cc") final String cc,
107                                               @PluginAttr("bcc") final String bcc,
108                                               @PluginAttr("from") final String from,
109                                               @PluginAttr("replyTo") final String replyTo,
110                                               @PluginAttr("subject") final String subject,
111                                               @PluginAttr("smtpProtocol") final String smtpProtocol,
112                                               @PluginAttr("smtpHost") final String smtpHost,
113                                               @PluginAttr("smtpPort") final String smtpPortNum,
114                                               @PluginAttr("smtpUsername") final String smtpUsername,
115                                               @PluginAttr("smtpPassword") final String smtpPassword,
116                                               @PluginAttr("smtpDebug") final String smtpDebug,
117                                               @PluginAttr("bufferSize") final String bufferSizeNum,
118                                               @PluginElement("layout") Layout<?> layout,
119                                               @PluginElement("filter") Filter filter,
120                                               @PluginAttr("suppressExceptions") final String suppressExceptions) {
121         if (name == null) {
122             LOGGER.error("No name provided for SMTPAppender");
123             return null;
124         }
125 
126         final boolean isHandleExceptions = suppressExceptions == null ? true : Boolean.valueOf(suppressExceptions);
127         final int smtpPort = smtpPortNum == null ? 0 : Integer.parseInt(smtpPortNum);
128         final boolean isSmtpDebug = smtpDebug == null ? false : Boolean.valueOf(smtpDebug);
129         final int bufferSize = bufferSizeNum == null ? DEFAULT_BUFFER_SIZE : Integer.valueOf(bufferSizeNum);
130 
131         if (layout == null) {
132             layout = HTMLLayout.createLayout(null, null, null, null, null, null);
133         }
134         if (filter == null) {
135             filter = ThresholdFilter.createFilter(null, null, null);
136         }
137 
138         final SMTPManager manager = SMTPManager.getSMTPManager(to, cc, bcc, from, replyTo, subject, smtpProtocol,
139             smtpHost, smtpPort, smtpUsername, smtpPassword, isSmtpDebug, filter.toString(),  bufferSize);
140         if (manager == null) {
141             return null;
142         }
143 
144         return new SMTPAppender(name, filter, layout, manager, isHandleExceptions);
145     }
146 
147     /**
148      * Capture all events in CyclicBuffer.
149      * @param event The Log event.
150      * @return true if the event should be filtered.
151      */
152     @Override
153     public boolean isFiltered(final LogEvent event) {
154         final boolean filtered = super.isFiltered(event);
155         if (filtered) {
156             manager.add(event);
157         }
158         return filtered;
159     }
160 
161     /**
162      * Perform SMTPAppender specific appending actions, mainly adding the event
163      * to a cyclic buffer and checking if the event triggers an e-mail to be
164      * sent.
165      * @param event The Log event.
166      */
167     public void append(final LogEvent event) {
168         manager.sendEvents(getLayout(), event);
169     }
170 }