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 java.io.Serializable;
21  
22  import org.apache.logging.log4j.core.Filter;
23  import org.apache.logging.log4j.core.Layout;
24  import org.apache.logging.log4j.core.LogEvent;
25  import org.apache.logging.log4j.core.config.Configuration;
26  import org.apache.logging.log4j.core.config.DefaultConfiguration;
27  import org.apache.logging.log4j.core.config.plugins.Plugin;
28  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
29  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
30  import org.apache.logging.log4j.core.config.plugins.PluginElement;
31  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
32  import org.apache.logging.log4j.core.filter.ThresholdFilter;
33  import org.apache.logging.log4j.core.layout.HtmlLayout;
34  import org.apache.logging.log4j.core.net.SmtpManager;
35  import org.apache.logging.log4j.core.util.Booleans;
36  
37  /**
38   * Send an e-mail when a specific logging event occurs, typically on errors or
39   * fatal errors.
40   *
41   * <p>
42   * The number of logging events delivered in this e-mail depend on the value of
43   * <b>BufferSize</b> option. The <code>SmtpAppender</code> keeps only the last
44   * <code>BufferSize</code> logging events in its cyclic buffer. This keeps
45   * memory requirements at a reasonable level while still delivering useful
46   * application context.
47   *
48   * By default, an email message will formatted as HTML. This can be modified by
49   * setting a layout for the appender.
50   *
51   * By default, an email message will be sent when an ERROR or higher severity
52   * message is appended. This can be modified by setting a filter for the
53   * appender.
54   */
55  @Plugin(name = "SMTP", category = "Core", elementType = "appender", printObject = true)
56  public final class SmtpAppender extends AbstractAppender {
57  
58      private static final int DEFAULT_BUFFER_SIZE = 512;
59  
60      /** The SMTP Manager */
61      private final SmtpManager manager;
62  
63      private SmtpAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, final SmtpManager manager,
64                           final boolean ignoreExceptions) {
65          super(name, filter, layout, ignoreExceptions);
66          this.manager = manager;
67      }
68  
69      /**
70       * Create a SmtpAppender.
71       *
72       * @param name
73       *            The name of the Appender.
74       * @param to
75       *            The comma-separated list of recipient email addresses.
76       * @param cc
77       *            The comma-separated list of CC email addresses.
78       * @param bcc
79       *            The comma-separated list of BCC email addresses.
80       * @param from
81       *            The email address of the sender.
82       * @param replyTo
83       *            The comma-separated list of reply-to email addresses.
84       * @param subject The subject of the email message.
85       * @param smtpProtocol The SMTP transport protocol (such as "smtps", defaults to "smtp").
86       * @param smtpHost
87       *            The SMTP hostname to send to.
88       * @param smtpPortStr
89       *            The SMTP port to send to.
90       * @param smtpUsername
91       *            The username required to authenticate against the SMTP server.
92       * @param smtpPassword
93       *            The password required to authenticate against the SMTP server.
94       * @param smtpDebug
95       *            Enable mail session debuging on STDOUT.
96       * @param bufferSizeStr
97       *            How many log events should be buffered for inclusion in the
98       *            message?
99       * @param layout
100      *            The layout to use (defaults to HtmlLayout).
101      * @param filter
102      *            The Filter or null (defaults to ThresholdFilter, level of
103      *            ERROR).
104      * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
105      *               they are propagated to the caller.
106      * @return The SmtpAppender.
107      */
108     @PluginFactory
109     public static SmtpAppender createAppender(
110             @PluginConfiguration final Configuration config,
111             @PluginAttribute("name") final String name,
112             @PluginAttribute("to") final String to,
113             @PluginAttribute("cc") final String cc,
114             @PluginAttribute("bcc") final String bcc,
115             @PluginAttribute("from") final String from,
116             @PluginAttribute("replyTo") final String replyTo,
117             @PluginAttribute("subject") final String subject,
118             @PluginAttribute("smtpProtocol") final String smtpProtocol,
119             @PluginAttribute("smtpHost") final String smtpHost,
120             @PluginAttribute("smtpPort") final String smtpPortStr,
121             @PluginAttribute("smtpUsername") final String smtpUsername,
122             @PluginAttribute("smtpPassword") final String smtpPassword,
123             @PluginAttribute("smtpDebug") final String smtpDebug,
124             @PluginAttribute("bufferSize") final String bufferSizeStr,
125             @PluginElement("Layout") Layout<? extends Serializable> layout,
126             @PluginElement("Filter") Filter filter,
127             @PluginAttribute("ignoreExceptions") final String ignore) {
128         if (name == null) {
129             LOGGER.error("No name provided for SmtpAppender");
130             return null;
131         }
132 
133         final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
134         final int smtpPort = AbstractAppender.parseInt(smtpPortStr, 0);
135         final boolean isSmtpDebug = Boolean.parseBoolean(smtpDebug);
136         final int bufferSize = bufferSizeStr == null ? DEFAULT_BUFFER_SIZE : Integer.parseInt(bufferSizeStr);
137 
138         if (layout == null) {
139             layout = HtmlLayout.createDefaultLayout();
140         }
141         if (filter == null) {
142             filter = ThresholdFilter.createFilter(null, null, null);
143         }
144         final Configuration configuration = config != null ? config : new DefaultConfiguration();
145 
146         final SmtpManager manager = SmtpManager.getSmtpManager(configuration, to, cc, bcc, from, replyTo, subject, smtpProtocol,
147             smtpHost, smtpPort, smtpUsername, smtpPassword, isSmtpDebug, filter.toString(),  bufferSize);
148         if (manager == null) {
149             return null;
150         }
151 
152         return new SmtpAppender(name, filter, layout, manager, ignoreExceptions);
153     }
154 
155     /**
156      * Capture all events in CyclicBuffer.
157      * @param event The Log event.
158      * @return true if the event should be filtered.
159      */
160     @Override
161     public boolean isFiltered(final LogEvent event) {
162         final boolean filtered = super.isFiltered(event);
163         if (filtered) {
164             manager.add(event);
165         }
166         return filtered;
167     }
168 
169     /**
170      * Perform SmtpAppender specific appending actions, mainly adding the event
171      * to a cyclic buffer and checking if the event triggers an e-mail to be
172      * sent.
173      * @param event The Log event.
174      */
175     @Override
176     public void append(final LogEvent event) {
177         manager.sendEvents(getLayout(), event);
178     }
179 }