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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
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 @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
149
150
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
163
164
165
166
167 public void append(final LogEvent event) {
168 manager.sendEvents(getLayout(), event);
169 }
170 }