001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018package org.apache.logging.log4j.core.appender; 019 020import java.io.Serializable; 021 022import org.apache.logging.log4j.core.Filter; 023import org.apache.logging.log4j.core.Layout; 024import org.apache.logging.log4j.core.LogEvent; 025import org.apache.logging.log4j.core.config.Configuration; 026import org.apache.logging.log4j.core.config.DefaultConfiguration; 027import org.apache.logging.log4j.core.config.plugins.Plugin; 028import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 029import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 030import org.apache.logging.log4j.core.config.plugins.PluginElement; 031import org.apache.logging.log4j.core.config.plugins.PluginFactory; 032import org.apache.logging.log4j.core.filter.ThresholdFilter; 033import org.apache.logging.log4j.core.layout.HtmlLayout; 034import org.apache.logging.log4j.core.net.SmtpManager; 035import org.apache.logging.log4j.core.util.Booleans; 036 037/** 038 * Send an e-mail when a specific logging event occurs, typically on errors or 039 * fatal errors. 040 * 041 * <p> 042 * The number of logging events delivered in this e-mail depend on the value of 043 * <b>BufferSize</b> option. The <code>SmtpAppender</code> keeps only the last 044 * <code>BufferSize</code> logging events in its cyclic buffer. This keeps 045 * memory requirements at a reasonable level while still delivering useful 046 * application context. 047 * 048 * By default, an email message will formatted as HTML. This can be modified by 049 * setting a layout for the appender. 050 * 051 * By default, an email message will be sent when an ERROR or higher severity 052 * message is appended. This can be modified by setting a filter for the 053 * appender. 054 */ 055@Plugin(name = "SMTP", category = "Core", elementType = "appender", printObject = true) 056public final class SmtpAppender extends AbstractAppender { 057 058 private static final int DEFAULT_BUFFER_SIZE = 512; 059 060 /** The SMTP Manager */ 061 private final SmtpManager manager; 062 063 private SmtpAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, final SmtpManager manager, 064 final boolean ignoreExceptions) { 065 super(name, filter, layout, ignoreExceptions); 066 this.manager = manager; 067 } 068 069 /** 070 * Create a SmtpAppender. 071 * 072 * @param name 073 * The name of the Appender. 074 * @param to 075 * The comma-separated list of recipient email addresses. 076 * @param cc 077 * The comma-separated list of CC email addresses. 078 * @param bcc 079 * The comma-separated list of BCC email addresses. 080 * @param from 081 * The email address of the sender. 082 * @param replyTo 083 * The comma-separated list of reply-to email addresses. 084 * @param subject The subject of the email message. 085 * @param smtpProtocol The SMTP transport protocol (such as "smtps", defaults to "smtp"). 086 * @param smtpHost 087 * The SMTP hostname to send to. 088 * @param smtpPortStr 089 * The SMTP port to send to. 090 * @param smtpUsername 091 * The username required to authenticate against the SMTP server. 092 * @param smtpPassword 093 * The password required to authenticate against the SMTP server. 094 * @param smtpDebug 095 * Enable mail session debuging on STDOUT. 096 * @param bufferSizeStr 097 * How many log events should be buffered for inclusion in the 098 * message? 099 * @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}