-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultMuteableConnectionLogger.java
228 lines (196 loc) · 7.76 KB
/
DefaultMuteableConnectionLogger.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.monitoring.logs;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.LogEntry;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.connectivity.service.util.ConnectivityMdcEntryKey;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
/**
* Default implementation of {@link MuteableConnectionLogger}.
* This implementation is not threadsafe since it ain't really of a big importance if a log message gets lost
* during activation of the logger.
* <p>
* In case of an exception this implementation switches it's delegate to an {@link ExceptionalConnectionLogger}.
*/
@NotThreadSafe
final class DefaultMuteableConnectionLogger implements MuteableConnectionLogger {
private final ThreadSafeDittoLogger logger;
private final ConnectionId connectionId;
private ConnectionLogger delegate;
private boolean active;
/**
* Create a new mutable connection logger that is currently muted.
*
* @param connectionId the connection for which the logger is logging.
* @param delegate the delegate to call while the logger is unmuted
*/
DefaultMuteableConnectionLogger(final ConnectionId connectionId, final ConnectionLogger delegate) {
this.connectionId = connectionId;
final var threadSafeLogger = DittoLoggerFactory.getThreadSafeLogger(DefaultMuteableConnectionLogger.class);
logger = threadSafeLogger.withMdcEntry(ConnectivityMdcEntryKey.CONNECTION_ID.toString(), connectionId);
this.delegate = delegate;
active = false;
}
@Override
public void mute() {
logTrace("Muting the logger");
active = false;
}
private void logTrace(final String message) {
if (logger.isTraceEnabled()) {
logger.trace(message);
}
}
@Override
public void unmute() {
logTrace("Unmuting the logger");
active = true;
}
@Override
public boolean isMuted() {
return !active;
}
@Override
public void success(final ConnectionMonitor.InfoProvider infoProvider) {
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging success since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.success(infoProvider));
}
}
@Override
public void success(final ConnectionMonitor.InfoProvider infoProvider, final String message,
final Object... messageArguments) {
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging success since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.success(infoProvider, message, messageArguments));
}
}
@Override
public void failure(final ConnectionMonitor.InfoProvider infoProvider,
@Nullable final DittoRuntimeException exception) {
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging failure since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.failure(infoProvider, exception));
}
}
@Override
public void failure(final ConnectionMonitor.InfoProvider infoProvider, final String message,
final Object... messageArguments) {
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging failure since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.failure(infoProvider, message, messageArguments));
}
}
@Override
public void exception(final ConnectionMonitor.InfoProvider infoProvider, @Nullable final Throwable exception) {
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging exception since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.exception(infoProvider, exception));
}
}
@Override
public void exception(final ConnectionMonitor.InfoProvider infoProvider, final String message,
final Object... messageArguments) {
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging exception since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.exception(infoProvider, message, messageArguments));
}
}
@Override
public void clear() {
wrapInExceptionHandling(() -> delegate.clear());
}
@Override
public void close() throws IOException {
delegate.close();
}
@Override
public void logEntry(final LogEntry logEntry) {
ConditionChecker.checkNotNull(logEntry, "logEntry");
if (isMuted()) {
logTraceWithCorrelationId(logEntry.getCorrelationId(), "Not logging entry since logger is muted.");
} else {
wrapInExceptionHandling(() -> delegate.logEntry(logEntry));
}
}
@Override
public Collection<LogEntry> getLogs() {
final Collection<LogEntry> result;
if (isMuted()) {
logger.trace("Returning empty logs since logger is muted.");
result = Collections.emptyList();
} else {
result = delegate.getLogs();
}
return result;
}
private void wrapInExceptionHandling(final Runnable runnable) {
try {
runnable.run();
} catch (final Exception e) {
logger.error("Encountered exception: <{}> in connection logger: <{}>. Switching delegate to: <{}>.",
e,
this,
ExceptionalConnectionLogger.class.getSimpleName());
delegate = ConnectionLoggerFactory.newExceptionalLogger(connectionId, e);
}
}
private void logTraceWithCorrelationId(final CharSequence correlationId,
final String message,
final Object... messageArguments) {
if (logger.isTraceEnabled()) {
logger.withCorrelationId(correlationId).trace(message, messageArguments);
}
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (DefaultMuteableConnectionLogger) o;
return active == that.active &&
Objects.equals(connectionId, that.connectionId) &&
Objects.equals(delegate, that.delegate);
}
@Override
public int hashCode() {
return Objects.hash(connectionId, delegate, active);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"connectionId=" + connectionId +
", delegate=" + delegate +
", active=" + active +
"]";
}
}