/src/logging-log4cxx/src/main/include/log4cxx/net/smtpappender.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef _LOG4CXX_NET_SMTP_H |
19 | | #define _LOG4CXX_NET_SMTP_H |
20 | | |
21 | | |
22 | | #include <log4cxx/appenderskeleton.h> |
23 | | #include <log4cxx/helpers/cyclicbuffer.h> |
24 | | #include <log4cxx/spi/triggeringeventevaluator.h> |
25 | | |
26 | | namespace LOG4CXX_NS |
27 | | { |
28 | | namespace net |
29 | | { |
30 | | /** |
31 | | Send an e-mail when a specific logging event occurs, typically when |
32 | | an <b>ERROR</b> level logging event is sent to the appender. |
33 | | |
34 | | A value must be provided for the following <b>param</b> elements : |
35 | | - <b>smtpHost</b> - |
36 | | The URL or IP address of the SMTP server. |
37 | | - <b>from</b> - |
38 | | The email address in the <b>from</b> field of the message. |
39 | | - one of <b>to</b>, <b>cc</b>, <b>bcc</b> - |
40 | | An email address in the message. |
41 | | |
42 | | The following <b>param</b> elements are optional: |
43 | | - <b>smtpPort</b> - |
44 | | The TCP/IP port number on the SMTP server. |
45 | | By default port 25 is assumed. |
46 | | - <b>subject</b> - |
47 | | Content for the the <b>subject</b> field of the message. |
48 | | - <b>smtpUsername</b> - |
49 | | Provided when the SMTP server requests authentication. |
50 | | - <b>smtpPassword</b> - |
51 | | Provided when the SMTP server requests authentication. |
52 | | - <b>BufferSize</b> - |
53 | | The number of logging events delivered in an e-mail. |
54 | | The <code>SMTPAppender</code> keeps only the last |
55 | | <code>BufferSize</code> logging events in its cyclic buffer. This |
56 | | keeps memory requirements at a reasonable level while still |
57 | | delivering useful application context. |
58 | | By default 512 logging events are kept in its cyclic buffer. |
59 | | - <b>evaluatorClass</b> - |
60 | | The registered spi::TriggeringEventEvaluator sub-class |
61 | | that provides the <code>isTriggeringEvent</code> implementation. |
62 | | This attribute can also be set using the <b>triggeringPolicy</b> element. |
63 | | By default an email is sent |
64 | | when the level of the logging event |
65 | | is greater or equal to <b>ERROR</b>. |
66 | | |
67 | | An example configuration is: |
68 | | \include async-example.xml |
69 | | |
70 | | */ |
71 | | class LOG4CXX_EXPORT SMTPAppender : public AppenderSkeleton |
72 | | { |
73 | | private: |
74 | | struct SMTPPriv; |
75 | | SMTPAppender(const SMTPAppender&); |
76 | | SMTPAppender& operator=(const SMTPAppender&); |
77 | | static bool asciiCheck(const LogString& value, const LogString& label); |
78 | | |
79 | | /** |
80 | | This method determines if there is a sense in attempting to append. |
81 | | <p>It checks whether there is a set output target and also if |
82 | | there is a set layout. If these checks fail, then the boolean |
83 | | value <code>false</code> is returned. */ |
84 | | bool checkEntryConditions(); |
85 | | |
86 | | public: |
87 | | DECLARE_LOG4CXX_OBJECT(SMTPAppender) |
88 | 0 | BEGIN_LOG4CXX_CAST_MAP() |
89 | 0 | LOG4CXX_CAST_ENTRY(SMTPAppender) |
90 | 0 | LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton) |
91 | 0 | END_LOG4CXX_CAST_MAP() |
92 | | |
93 | | SMTPAppender(); |
94 | | /** |
95 | | The default constructor will instantiate the appender with a |
96 | | spi::TriggeringEventEvaluator that will trigger on events with |
97 | | level ERROR or higher.*/ |
98 | | SMTPAppender(LOG4CXX_NS::helpers::Pool& p); |
99 | | |
100 | | /** |
101 | | Use <code>evaluator</code> passed as parameter as the |
102 | | spi::TriggeringEventEvaluator for this net::SMTPAppender. |
103 | | */ |
104 | | SMTPAppender(spi::TriggeringEventEvaluatorPtr evaluator); |
105 | | |
106 | | ~SMTPAppender(); |
107 | | |
108 | | /** |
109 | | \copybrief AppenderSkeleton::setOption() |
110 | | |
111 | | Supported options | Supported values | Default value | |
112 | | -------------- | ---------------- | --------------- | |
113 | | smtpHost | {any} | - | |
114 | | smtpPort | {int} | 25 | |
115 | | smtpUserName | {any} | - | |
116 | | smtpPassword | {any} | - | |
117 | | from | (\ref asciiCheck "1") | - | |
118 | | to | (\ref asciiCheck "1") | - | |
119 | | cc | (\ref asciiCheck "1") | - | |
120 | | bcc | (\ref asciiCheck "1") | - | |
121 | | subject | {any} | - | |
122 | | subject | {any} | - | |
123 | | buffersize | {int} | 512 | |
124 | | evaluatorClass | (\ref AppenderSkeleton "2") | - | |
125 | | |
126 | | \anchor asciiCheck (1) Only ASCII charaters |
127 | | |
128 | | \anchor TriggeringEventEvaluator (2) A registered class deriving from TriggeringEventEvaluator |
129 | | |
130 | | \sa AppenderSkeleton::setOption() |
131 | | */ |
132 | | void setOption(const LogString& option, const LogString& value) override; |
133 | | |
134 | | /** |
135 | | \copybrief AppenderSkeleton::activateOptions() |
136 | | |
137 | | Will not activate and will log an error message if:<ul> |
138 | | <li>no layout is provided</li> |
139 | | <li>no TriggeringEventEvaluator is provided</li> |
140 | | <li>any required field is missing</li> |
141 | | <li>a non-ascii character is detected where not permitted</li> |
142 | | </ul>. |
143 | | */ |
144 | | void activateOptions(helpers::Pool& p) override; |
145 | | |
146 | | /** |
147 | | Perform SMTPAppender specific appending actions, mainly adding |
148 | | the event to a cyclic buffer and checking if the event triggers |
149 | | an e-mail to be sent. */ |
150 | | void append(const spi::LoggingEventPtr& event, helpers::Pool& p) override; |
151 | | |
152 | | |
153 | | void close() override; |
154 | | |
155 | | /** |
156 | | Returns value of the <b>To</b> option. |
157 | | */ |
158 | | LogString getTo() const; |
159 | | |
160 | | /** |
161 | | Returns value of the <b>cc</b> option. |
162 | | */ |
163 | | LogString getCc() const; |
164 | | |
165 | | /** |
166 | | Returns value of the <b>bcc</b> option. |
167 | | */ |
168 | | LogString getBcc() const; |
169 | | |
170 | | |
171 | | /** |
172 | | The <code>SMTPAppender</code> requires a <code>Layout</code>. |
173 | | */ |
174 | | bool requiresLayout() const override; |
175 | | |
176 | | /** |
177 | | Send the contents of the cyclic buffer as an e-mail message. |
178 | | */ |
179 | | void sendBuffer(LOG4CXX_NS::helpers::Pool& p); |
180 | | |
181 | | |
182 | | /** |
183 | | Returns value of the <b>EvaluatorClass</b> option. |
184 | | */ |
185 | | LogString getEvaluatorClass(); |
186 | | |
187 | | /** |
188 | | Returns value of the <b>From</b> option. |
189 | | */ |
190 | | LogString getFrom() const; |
191 | | |
192 | | /** |
193 | | Returns value of the <b>Subject</b> option. |
194 | | */ |
195 | | LogString getSubject() const; |
196 | | |
197 | | |
198 | | /** |
199 | | The <b>From</b> option takes a string value which should be a |
200 | | e-mail address of the sender. |
201 | | */ |
202 | | void setFrom(const LogString& from); |
203 | | |
204 | | /** |
205 | | The <b>Subject</b> option takes a string value which should be a |
206 | | the subject of the e-mail message. |
207 | | */ |
208 | | void setSubject(const LogString& subject); |
209 | | |
210 | | /** |
211 | | The <b>BufferSize</b> option takes a positive integer |
212 | | representing the maximum number of logging events to collect in a |
213 | | cyclic buffer. When the <code>BufferSize</code> is reached, |
214 | | oldest events are deleted as new events are added to the |
215 | | buffer. By default the size of the cyclic buffer is 512 events. |
216 | | */ |
217 | | void setBufferSize(int bufferSize); |
218 | | |
219 | | /** |
220 | | The <b>SMTPHost</b> option takes a string value which should be a |
221 | | the host name of the SMTP server that will send the e-mail message. |
222 | | */ |
223 | | void setSMTPHost(const LogString& smtpHost); |
224 | | |
225 | | /** |
226 | | Returns value of the <b>SMTPHost</b> option. |
227 | | */ |
228 | | LogString getSMTPHost() const; |
229 | | |
230 | | /** |
231 | | The <b>SMTPPort</b> option takes a string value which should be a |
232 | | the port of the SMTP server that will send the e-mail message. |
233 | | */ |
234 | | void setSMTPPort(int port); |
235 | | |
236 | | /** |
237 | | Returns value of the <b>SMTPHost</b> option. |
238 | | */ |
239 | | int getSMTPPort() const; |
240 | | |
241 | | /** |
242 | | The <b>To</b> option takes a string value which should be a |
243 | | comma separated list of e-mail address of the recipients. |
244 | | */ |
245 | | void setTo(const LogString& to); |
246 | | |
247 | | /** |
248 | | The <b>Cc</b> option takes a string value which should be a |
249 | | comma separated list of e-mail address of the cc'd recipients. |
250 | | */ |
251 | | void setCc(const LogString& to); |
252 | | |
253 | | /** |
254 | | The <b>Bcc</b> option takes a string value which should be a |
255 | | comma separated list of e-mail address of the bcc'd recipients. |
256 | | */ |
257 | | void setBcc(const LogString& to); |
258 | | |
259 | | |
260 | | /** |
261 | | The <b>SMTPUsername</b> option takes a string value which should be a |
262 | | the user name for the SMTP server. |
263 | | */ |
264 | | void setSMTPUsername(const LogString& newVal); |
265 | | |
266 | | /** |
267 | | Returns value of the <b>SMTPUsername</b> option. |
268 | | */ |
269 | | LogString getSMTPUsername() const; |
270 | | |
271 | | /** |
272 | | The <b>SMTPPassword</b> option takes a string value which should be a |
273 | | the password for the SMTP server. |
274 | | */ |
275 | | void setSMTPPassword(const LogString& newVal); |
276 | | |
277 | | /** |
278 | | Returns value of the <b>SMTPPassword</b> option. |
279 | | */ |
280 | | LogString getSMTPPassword() const; |
281 | | |
282 | | /** |
283 | | Returns value of the <b>BufferSize</b> option. |
284 | | */ |
285 | | int getBufferSize() const; |
286 | | |
287 | | |
288 | | /** |
289 | | * Gets the current triggering evaluator. |
290 | | * @return triggering evaluator. |
291 | | */ |
292 | | LOG4CXX_NS::spi::TriggeringEventEvaluatorPtr getEvaluator() const; |
293 | | |
294 | | /** |
295 | | * Sets the triggering evaluator. |
296 | | * @param trigger triggering evaluator. |
297 | | */ |
298 | | void setEvaluator(LOG4CXX_NS::spi::TriggeringEventEvaluatorPtr& trigger); |
299 | | |
300 | | /** |
301 | | The <b>EvaluatorClass</b> option takes a string value |
302 | | representing the name of the class implementing the |
303 | | spi::TriggeringEventEvaluator interface. A corresponding object will |
304 | | be instantiated and assigned as the triggering event evaluator |
305 | | for the SMTPAppender. |
306 | | */ |
307 | | void setEvaluatorClass(const LogString& value); |
308 | | |
309 | | /** |
310 | | The <b>LocationInfo</b> option is provided for compatibility with log4j |
311 | | and has no effect in log4cxx. |
312 | | */ |
313 | | void setLocationInfo(bool locationInfo); |
314 | | |
315 | | /** |
316 | | Returns value of the <b>LocationInfo</b> option. |
317 | | */ |
318 | | bool getLocationInfo() const; |
319 | | }; // class SMTPAppender |
320 | | |
321 | | LOG4CXX_PTR_DEF(SMTPAppender); |
322 | | |
323 | | } // namespace net |
324 | | } // namespace log4cxx |
325 | | |
326 | | #endif // _LOG4CXX_NET_SMTP_H |