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 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.plugins.Plugin;
26 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27 import org.apache.logging.log4j.core.config.plugins.PluginElement;
28 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
29 import org.apache.logging.log4j.core.filter.ThresholdFilter;
30 import org.apache.logging.log4j.core.helpers.Booleans;
31 import org.apache.logging.log4j.core.layout.HTMLLayout;
32 import org.apache.logging.log4j.core.net.SMTPManager;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 @Plugin(name = "SMTP", category = "Core", elementType = "appender", printObject = true)
53 public final class SMTPAppender extends AbstractAppender {
54
55 private static final int DEFAULT_BUFFER_SIZE = 512;
56
57
58 protected final SMTPManager manager;
59
60 private SMTPAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, final SMTPManager manager,
61 final boolean ignoreExceptions) {
62 super(name, filter, layout, ignoreExceptions);
63 this.manager = manager;
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 SMTPAppender createAppender(
107 @PluginAttribute("name") final String name,
108 @PluginAttribute("to") final String to,
109 @PluginAttribute("cc") final String cc,
110 @PluginAttribute("bcc") final String bcc,
111 @PluginAttribute("from") final String from,
112 @PluginAttribute("replyTo") final String replyTo,
113 @PluginAttribute("subject") final String subject,
114 @PluginAttribute("smtpProtocol") final String smtpProtocol,
115 @PluginAttribute("smtpHost") final String smtpHost,
116 @PluginAttribute("smtpPort") final String smtpPortStr,
117 @PluginAttribute("smtpUsername") final String smtpUsername,
118 @PluginAttribute("smtpPassword") final String smtpPassword,
119 @PluginAttribute("smtpDebug") final String smtpDebug,
120 @PluginAttribute("bufferSize") final String bufferSizeStr,
121 @PluginElement("Layout") Layout<? extends Serializable> layout,
122 @PluginElement("Filter") Filter filter,
123 @PluginAttribute("ignoreExceptions") final String ignore) {
124 if (name == null) {
125 LOGGER.error("No name provided for SMTPAppender");
126 return null;
127 }
128
129 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
130 final int smtpPort = AbstractAppender.parseInt(smtpPortStr, 0);
131 final boolean isSmtpDebug = Boolean.parseBoolean(smtpDebug);
132 final int bufferSize = bufferSizeStr == null ? DEFAULT_BUFFER_SIZE : Integer.parseInt(bufferSizeStr);
133
134 if (layout == null) {
135 layout = HTMLLayout.createLayout(null, null, null, null, null, null);
136 }
137 if (filter == null) {
138 filter = ThresholdFilter.createFilter(null, null, null);
139 }
140
141 final SMTPManager manager = SMTPManager.getSMTPManager(to, cc, bcc, from, replyTo, subject, smtpProtocol,
142 smtpHost, smtpPort, smtpUsername, smtpPassword, isSmtpDebug, filter.toString(), bufferSize);
143 if (manager == null) {
144 return null;
145 }
146
147 return new SMTPAppender(name, filter, layout, manager, ignoreExceptions);
148 }
149
150
151
152
153
154
155 @Override
156 public boolean isFiltered(final LogEvent event) {
157 final boolean filtered = super.isFiltered(event);
158 if (filtered) {
159 manager.add(event);
160 }
161 return filtered;
162 }
163
164
165
166
167
168
169
170 @Override
171 public void append(final LogEvent event) {
172 manager.sendEvents(getLayout(), event);
173 }
174 }