1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 import java.io.Serializable;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @Plugin(name = "SMTP", category = "Core", elementType = "appender", printObject = true)
52 public final class SMTPAppender<T extends Serializable> extends AbstractAppender<T> {
53
54 private static final int DEFAULT_BUFFER_SIZE = 512;
55
56
57 protected final SMTPManager manager;
58
59 private SMTPAppender(final String name, final Filter filter, final Layout<T> layout, final SMTPManager manager,
60 final boolean handleExceptions) {
61 super(name, filter, layout, handleExceptions);
62 this.manager = manager;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @PluginFactory
106 public static <S extends Serializable> SMTPAppender<S> createAppender(@PluginAttr("name") final String name,
107 @PluginAttr("to") final String to,
108 @PluginAttr("cc") final String cc,
109 @PluginAttr("bcc") final String bcc,
110 @PluginAttr("from") final String from,
111 @PluginAttr("replyTo") final String replyTo,
112 @PluginAttr("subject") final String subject,
113 @PluginAttr("smtpProtocol") final String smtpProtocol,
114 @PluginAttr("smtpHost") final String smtpHost,
115 @PluginAttr("smtpPort") final String smtpPortNum,
116 @PluginAttr("smtpUsername") final String smtpUsername,
117 @PluginAttr("smtpPassword") final String smtpPassword,
118 @PluginAttr("smtpDebug") final String smtpDebug,
119 @PluginAttr("bufferSize") final String bufferSizeNum,
120 @PluginElement("layout") Layout<S> layout,
121 @PluginElement("filter") Filter filter,
122 @PluginAttr("suppressExceptions") final String suppressExceptions) {
123 if (name == null) {
124 LOGGER.error("No name provided for SMTPAppender");
125 return null;
126 }
127
128 final boolean isHandleExceptions = suppressExceptions == null ? true : Boolean.valueOf(suppressExceptions);
129 final int smtpPort = smtpPortNum == null ? 0 : Integer.parseInt(smtpPortNum);
130 final boolean isSmtpDebug = smtpDebug == null ? false : Boolean.valueOf(smtpDebug);
131 final int bufferSize = bufferSizeNum == null ? DEFAULT_BUFFER_SIZE : Integer.valueOf(bufferSizeNum);
132
133 if (layout == null) {
134 @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
135 Layout<S> l = (Layout<S>)HTMLLayout.createLayout(null, null, null, null, null, null);
136 layout = l;
137 }
138 if (filter == null) {
139 filter = ThresholdFilter.createFilter(null, null, null);
140 }
141
142 final SMTPManager manager = SMTPManager.getSMTPManager(to, cc, bcc, from, replyTo, subject, smtpProtocol,
143 smtpHost, smtpPort, smtpUsername, smtpPassword, isSmtpDebug, filter.toString(), bufferSize);
144 if (manager == null) {
145 return null;
146 }
147
148 return new SMTPAppender<S>(name, filter, layout, manager, isHandleExceptions);
149 }
150
151
152
153
154
155
156 @Override
157 public boolean isFiltered(final LogEvent event) {
158 final boolean filtered = super.isFiltered(event);
159 if (filtered) {
160 manager.add(event);
161 }
162 return filtered;
163 }
164
165
166
167
168
169
170
171 @Override
172 public void append(final LogEvent event) {
173 manager.sendEvents(getLayout(), event);
174 }
175 }