/src/logging-log4cxx/src/main/include/log4cxx/asyncappender.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_ASYNC_APPENDER_H |
19 | | #define _LOG4CXX_ASYNC_APPENDER_H |
20 | | |
21 | | #include <log4cxx/appenderskeleton.h> |
22 | | #include <log4cxx/helpers/appenderattachableimpl.h> |
23 | | #include <log4cxx/spi/loggingevent.h> |
24 | | |
25 | | namespace LOG4CXX_NS |
26 | | { |
27 | | LOG4CXX_LIST_DEF(LoggingEventList, spi::LoggingEventPtr); |
28 | | |
29 | | /** |
30 | | The AsyncAppender decouples logging event creation from output |
31 | | by processing log events asynchronously. |
32 | | |
33 | | The AsyncAppender stores the logging event in a bounded buffer |
34 | | and then returns control to the application. |
35 | | A separate thread forwards events to the attached appender(s). |
36 | | |
37 | | The AsyncAppender is useful when outputting to a slow event sink, |
38 | | for example, a remote SMTP server or a database. |
39 | | Attaching a FileAppender to AsyncAppender |
40 | | to reduce logging overhead is not recommended |
41 | | as the inter-thread communication overhead |
42 | | can exceed the time to write directly to a file. |
43 | | |
44 | | You can attach multiple appenders to an AsyncAppender by: |
45 | | - calling AsyncAppender::addAppender repeatedly when progammatically configuring Log4cxx. |
46 | | - adding multiple <b>appender-ref</b> elements |
47 | | to the <b>appender class="AsyncAppender"</b> element |
48 | | when using runtime configuration. |
49 | | |
50 | | <b>Important note:</b> The <code>AsyncAppender</code> can only |
51 | | be configured at runtime using XML, |
52 | | i.e. when the application uses {@link xml::DOMConfigurator DOMConfigurator} |
53 | | to load the cofiguration file. |
54 | | |
55 | | Here is a sample configuration file: |
56 | | \include async-example.xml |
57 | | |
58 | | ### Configurable properties |
59 | | |
60 | | When the application produces logging events faster |
61 | | than the background thread is able to process, |
62 | | the bounded buffer can become full. |
63 | | In this situation AsyncAppender will either |
64 | | block until the bounded buffer has a free slot or |
65 | | discard the event. |
66 | | The <b>Blocking</b> property controls which behaviour is used. |
67 | | When events are discarded, |
68 | | the logged output will indicate this |
69 | | with a log message prefixed with <i>Discarded</i>. |
70 | | The output may contain one <i>Discarded</i> message per logger name, |
71 | | the logging event of the highest level for each logger |
72 | | whose events have been discarded. |
73 | | |
74 | | To determine whether the application produces logging events faster |
75 | | than the background thread is able to process, enable [Log4cxx internal debugging](internal-debugging.html). |
76 | | The AsyncAppender will output a histogram of queue length frequencies when closed. |
77 | | |
78 | | See AsyncAppender::setOption for details. |
79 | | |
80 | | */ |
81 | | class LOG4CXX_EXPORT AsyncAppender : |
82 | | public virtual spi::AppenderAttachable, |
83 | | public virtual AppenderSkeleton |
84 | | { |
85 | | protected: |
86 | | struct AsyncAppenderPriv; |
87 | | |
88 | | public: |
89 | | DECLARE_LOG4CXX_OBJECT(AsyncAppender) |
90 | 0 | BEGIN_LOG4CXX_CAST_MAP() |
91 | 0 | LOG4CXX_CAST_ENTRY(AsyncAppender) |
92 | 0 | LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton) |
93 | 0 | LOG4CXX_CAST_ENTRY(spi::AppenderAttachable) |
94 | 0 | END_LOG4CXX_CAST_MAP() |
95 | | |
96 | | /** |
97 | | * Create new instance. |
98 | | */ |
99 | | AsyncAppender(); |
100 | | |
101 | | /** |
102 | | * Destructor. |
103 | | */ |
104 | | virtual ~AsyncAppender(); |
105 | | |
106 | | /** |
107 | | * Ensure \c newAppender receives any logging event |
108 | | * added to this appender. |
109 | | * |
110 | | * @param newAppender appender to add, may not be null. |
111 | | */ |
112 | | void addAppender(const AppenderPtr newAppender) override; |
113 | | |
114 | | void doAppend(const spi::LoggingEventPtr& event, |
115 | | helpers::Pool& pool1) override; |
116 | | |
117 | | void append(const spi::LoggingEventPtr& event, helpers::Pool& p) override; |
118 | | |
119 | | /** |
120 | | Close this <code>AsyncAppender</code> by interrupting the |
121 | | dispatcher thread which will process all pending events before |
122 | | exiting. |
123 | | */ |
124 | | void close() override; |
125 | | |
126 | | /** |
127 | | * Get iterator over attached appenders. |
128 | | * @return list of all attached appenders. |
129 | | */ |
130 | | AppenderList getAllAppenders() const override; |
131 | | |
132 | | /** |
133 | | * Get appender by name. |
134 | | * |
135 | | * @param name name, may not be null. |
136 | | * @return matching appender or null. |
137 | | */ |
138 | | AppenderPtr getAppender(const LogString& name) const override; |
139 | | |
140 | | /** |
141 | | * The current value of the (unused) <b>LocationInfo</b> option. |
142 | | */ |
143 | | bool getLocationInfo() const; |
144 | | /** |
145 | | * Determines if specified appender is attached. |
146 | | * @param appender appender. |
147 | | * @return true if attached. |
148 | | */ |
149 | | bool isAttached(const AppenderPtr appender) const override; |
150 | | |
151 | | bool requiresLayout() const override; |
152 | | |
153 | | /** |
154 | | * Removes and closes all attached appenders. |
155 | | */ |
156 | | void removeAllAppenders() override; |
157 | | |
158 | | /** |
159 | | * Removes an appender. |
160 | | * @param appender appender to remove. |
161 | | */ |
162 | | void removeAppender(const AppenderPtr appender) override; |
163 | | /** |
164 | | * Remove appender by name. |
165 | | * @param name name. |
166 | | */ |
167 | | void removeAppender(const LogString& name) override; |
168 | | |
169 | | /** |
170 | | * Replace \c oldAppender with \c newAppender. |
171 | | * @return true if oldAppender was replaced with newAppender. |
172 | | */ |
173 | | bool replaceAppender(const AppenderPtr& oldAppender, const AppenderPtr& newAppender) LOG4CXX_16_VIRTUAL_SPECIFIER; |
174 | | |
175 | | /** |
176 | | * Replace any previously added appenders with \c newList. |
177 | | */ |
178 | | void replaceAppenders(const AppenderList& newList) LOG4CXX_16_VIRTUAL_SPECIFIER; |
179 | | |
180 | | /** |
181 | | * The <b>LocationInfo</b> attribute is provided for compatibility |
182 | | * with log4j and has no effect on the log output. |
183 | | * @param flag new value. |
184 | | */ |
185 | | void setLocationInfo(bool flag); |
186 | | |
187 | | /** |
188 | | * The <b>BufferSize</b> option takes a non-negative integer value. |
189 | | * This integer value determines the maximum size of the bounded |
190 | | * buffer. |
191 | | * */ |
192 | | void setBufferSize(int size); |
193 | | |
194 | | /** |
195 | | * Gets the current buffer size. |
196 | | * @return the current value of the <b>BufferSize</b> option. |
197 | | */ |
198 | | int getBufferSize() const; |
199 | | |
200 | | /** |
201 | | * Sets whether appender should wait if there is no |
202 | | * space available in the event buffer or immediately return. |
203 | | * |
204 | | * @param value true if appender should wait until available space in buffer. |
205 | | */ |
206 | | void setBlocking(bool value); |
207 | | |
208 | | /** |
209 | | * Gets whether appender should block calling thread when buffer is full. |
210 | | * If false, messages will be counted by logger and a summary |
211 | | * message appended after the contents of the buffer have been appended. |
212 | | * |
213 | | * @return true if calling thread will be blocked when buffer is full. |
214 | | */ |
215 | | bool getBlocking() const; |
216 | | |
217 | | |
218 | | /** |
219 | | \copybrief AppenderSkeleton::setOption() |
220 | | |
221 | | Supported options | Supported values | Default value |
222 | | -------------- | ---------------- | --------------- |
223 | | BufferSize | int | 128 |
224 | | Blocking | True,False | True |
225 | | |
226 | | \sa AppenderSkeleton::setOption() |
227 | | */ |
228 | | void setOption(const LogString& option, const LogString& value) override; |
229 | | |
230 | | |
231 | | private: |
232 | | AsyncAppender(const AsyncAppender&); |
233 | | AsyncAppender& operator=(const AsyncAppender&); |
234 | | |
235 | | /** |
236 | | * Dispatch routine. |
237 | | */ |
238 | | void dispatch(); |
239 | | |
240 | | }; // class AsyncAppender |
241 | | LOG4CXX_PTR_DEF(AsyncAppender); |
242 | | } // namespace log4cxx |
243 | | |
244 | | #endif// _LOG4CXX_ASYNC_APPENDER_H |
245 | | |