-
Notifications
You must be signed in to change notification settings - Fork 408
/
DelegatingAsyncDisruptorAppender.java
197 lines (169 loc) · 6.69 KB
/
DelegatingAsyncDisruptorAppender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.logstash.logback.appender;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import net.logstash.logback.appender.listener.AppenderListener;
import ch.qos.logback.access.common.spi.IAccessEvent;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AsyncAppenderBase;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.spi.AppenderAttachable;
import ch.qos.logback.core.spi.AppenderAttachableImpl;
import ch.qos.logback.core.spi.DeferredProcessingAware;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
/**
* An {@link AsyncDisruptorAppender} that delegates appending of an event
* to delegate {@link #appenders}.
*
* This is very similar to logback's {@link AsyncAppenderBase}, except that:
* <ul>
* <li>it uses a {@link RingBuffer} instead of a {@link BlockingQueue}</li>
* <li>it allows any number of delegate appenders, instead of just one</li>
* <li>it flushes appenders of type {@link OutputStreamAppender} or {@link Flushable} at the end of a batch</li>
* <li>it is resilient to exceptions and guarantees that all appenders are invoked</li>
* </ul>
*
* @param <Event> type of event ({@link ILoggingEvent} or {@link IAccessEvent}).
*/
public abstract class DelegatingAsyncDisruptorAppender<Event extends DeferredProcessingAware, Listener extends AppenderListener<Event>> extends AsyncDisruptorAppender<Event, Listener> implements AppenderAttachable<Event> {
/**
* The delegate appenders.
*/
private final AppenderAttachableImpl<Event> appenders = new AppenderAttachableImpl<>();
private class DelegatingEventHandler implements EventHandler<LogEvent<Event>> {
/**
* Whether exceptions should be reported with a error status or not.
*/
private boolean silentError;
@Override
public void onEvent(LogEvent<Event> logEvent, long sequence, boolean endOfBatch) throws Exception {
boolean exceptionThrown = false;
for (Iterator<Appender<Event>> it = appenders.iteratorForAppenders(); it.hasNext();) {
Appender<Event> appender = it.next();
try {
appender.doAppend(logEvent.event);
/*
* Optimization:
*
* If any of the delegate appenders are instances of OutputStreamAppender or Flushable,
* then flush them at the end of the batch.
*/
if (endOfBatch) {
flushAppender(appender);
}
} catch (Exception e) {
exceptionThrown = true;
if (!this.silentError) {
addError(String.format("Unable to forward event to appender [%s]: %s", appender.getName(), e.getMessage()), e);
}
}
}
this.silentError = exceptionThrown;
}
private void flushAppender(Appender<Event> appender) throws IOException {
// Similar to #doAppend() - don't flush if appender is stopped
if (!appender.isStarted()) {
return;
}
if (appender instanceof Flushable) {
flushAppender((Flushable) appender);
} else if (appender instanceof OutputStreamAppender) {
flushAppender((OutputStreamAppender<Event>) appender);
}
}
private void flushAppender(OutputStreamAppender<Event> appender) throws IOException {
if (!appender.isImmediateFlush()) {
OutputStream os = appender.getOutputStream();
if (os != null) {
os.flush();
}
}
}
private void flushAppender(Flushable appender) throws IOException {
appender.flush();
}
}
@Override
protected EventHandler<LogEvent<Event>> createEventHandler() {
return new DelegatingEventHandler();
}
@Override
public void start() {
startDelegateAppenders();
super.start();
}
@Override
public void stop() {
if (!isStarted()) {
return;
}
super.stop();
stopDelegateAppenders();
}
private void startDelegateAppenders() {
for (Iterator<Appender<Event>> appenderIter = appenders.iteratorForAppenders(); appenderIter.hasNext();) {
Appender<Event> appender = appenderIter.next();
if (appender.getContext() == null) {
appender.setContext(getContext());
}
if (!appender.isStarted()) {
appender.start();
}
}
}
private void stopDelegateAppenders() {
for (Iterator<Appender<Event>> appenderIter = appenders.iteratorForAppenders(); appenderIter.hasNext();) {
Appender<Event> appender = appenderIter.next();
if (appender.isStarted()) {
appender.stop();
}
}
}
@Override
public void addAppender(Appender<Event> newAppender) {
appenders.addAppender(newAppender);
}
@Override
public Iterator<Appender<Event>> iteratorForAppenders() {
return appenders.iteratorForAppenders();
}
@Override
public Appender<Event> getAppender(String name) {
return appenders.getAppender(name);
}
@Override
public boolean isAttached(Appender<Event> appender) {
return appenders.isAttached(appender);
}
@Override
public void detachAndStopAllAppenders() {
appenders.detachAndStopAllAppenders();
}
@Override
public boolean detachAppender(Appender<Event> appender) {
return appenders.detachAppender(appender);
}
@Override
public boolean detachAppender(String name) {
return appenders.detachAppender(name);
}
}