Skip to content

Commit

Permalink
Issue #106: Added method to directly log 'LogEntry' to 'ConnectionLog…
Browse files Browse the repository at this point in the history
…ger'.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Nov 11, 2021
1 parent f93ccd1 commit 0fb1522
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.ditto.connectivity.service.messaging.monitoring.logs;

import java.util.Collection;

import javax.annotation.Nullable;

import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.LogEntry;
import org.eclipse.ditto.connectivity.service.config.MonitoringLoggerConfig;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.base.model.signals.Signal;

/**
* Logger for connections that provides log messages for end users.
Expand Down Expand Up @@ -164,4 +163,13 @@ default void exception(final ConnectionMonitor.InfoProvider infoProvider) {
exception(infoProvider, null);
}

/**
* Logs the specified {@code LogEntry} argument.
*
* @param logEntry the entry to be logged.
* @throws NullPointerException if {@code logEntry} is {@code null}.
* @since 2.2.0
*/
void logEntry(LogEntry logEntry);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -51,8 +52,8 @@ final class DefaultMuteableConnectionLogger implements MuteableConnectionLogger
*/
DefaultMuteableConnectionLogger(final ConnectionId connectionId, final ConnectionLogger delegate) {
this.connectionId = connectionId;
logger = DittoLoggerFactory.getThreadSafeLogger(DefaultMuteableConnectionLogger.class)
.withMdcEntry(ConnectivityMdcEntryKey.CONNECTION_ID.toString(), connectionId);
final var threadSafeLogger = DittoLoggerFactory.getThreadSafeLogger(DefaultMuteableConnectionLogger.class);
logger = threadSafeLogger.withMdcEntry(ConnectivityMdcEntryKey.CONNECTION_ID.toString(), connectionId);
this.delegate = delegate;
active = false;
}
Expand All @@ -63,6 +64,12 @@ public void mute() {
active = false;
}

private void logTrace(final String message) {
if (logger.isTraceEnabled()) {
logger.trace(message);
}
}

@Override
public void unmute() {
logTrace("Unmuting the logger");
Expand All @@ -76,63 +83,63 @@ public boolean isMuted() {

@Override
public void success(final ConnectionMonitor.InfoProvider infoProvider) {
if (active) {
wrapInExceptionHandling(() -> delegate.success(infoProvider));
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging success since logger is muted.");
} else {
logTraceWithCorrelationId("Not logging success since logger is muted.", infoProvider);
wrapInExceptionHandling(() -> delegate.success(infoProvider));
}
}

@Override
public void success(final ConnectionMonitor.InfoProvider infoProvider, final String message,
final Object... messageArguments) {

if (active) {
wrapInExceptionHandling(() -> delegate.success(infoProvider, message, messageArguments));
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging success since logger is muted.");
} else {
logTraceWithCorrelationId("Not logging success since logger is muted.", infoProvider);
wrapInExceptionHandling(() -> delegate.success(infoProvider, message, messageArguments));
}
}

@Override
public void failure(final ConnectionMonitor.InfoProvider infoProvider,
@Nullable final DittoRuntimeException exception) {

if (active) {
wrapInExceptionHandling(() -> delegate.failure(infoProvider, exception));
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging failure since logger is muted.");
} else {
logTraceWithCorrelationId("Not logging failure since logger is muted.", infoProvider);
wrapInExceptionHandling(() -> delegate.failure(infoProvider, exception));
}
}

@Override
public void failure(final ConnectionMonitor.InfoProvider infoProvider, final String message,
final Object... messageArguments) {

if (active) {
wrapInExceptionHandling(() -> delegate.failure(infoProvider, message, messageArguments));
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging failure since logger is muted.");
} else {
logTraceWithCorrelationId("Not logging failure since logger is muted.", infoProvider);
wrapInExceptionHandling(() -> delegate.failure(infoProvider, message, messageArguments));
}
}

@Override
public void exception(final ConnectionMonitor.InfoProvider infoProvider, @Nullable final Exception exception) {
if (active) {
wrapInExceptionHandling(() -> delegate.exception(infoProvider, exception));
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging exception since logger is muted.");
} else {
logTraceWithCorrelationId("Not logging exception since logger is muted.", infoProvider);
wrapInExceptionHandling(() -> delegate.exception(infoProvider, exception));
}
}

@Override
public void exception(final ConnectionMonitor.InfoProvider infoProvider, final String message,
final Object... messageArguments) {

if (active) {
wrapInExceptionHandling(() -> delegate.exception(infoProvider, message, messageArguments));
if (isMuted()) {
logTraceWithCorrelationId(infoProvider.getCorrelationId(), "Not logging exception since logger is muted.");
} else {
logTraceWithCorrelationId("Not logging exception since logger is muted.", infoProvider);
wrapInExceptionHandling(() -> delegate.exception(infoProvider, message, messageArguments));
}
}

Expand All @@ -142,40 +149,48 @@ public void clear() {
}

@Override
public Collection<LogEntry> getLogs() {
if (active) {
return delegate.getLogs();
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));
}
logger.trace("Returning empty logs since logger is muted.");
return Collections.emptyList();
}

private void logTrace(final String message) {
if (logger.isTraceEnabled()) {
logger.trace(message);
@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());
logger.error("Encountered exception: <{}> in connection logger: <{}>. Switching delegate to: <{}>.",
e,
this,
ExceptionalConnectionLogger.class.getSimpleName());
delegate = ConnectionLoggerFactory.newExceptionalLogger(connectionId, e);
}
}

private void logTraceWithCorrelationId(final String message,
final ConnectionMonitor.InfoProvider infoProvider,
private void logTraceWithCorrelationId(final CharSequence correlationId,
final String message,
final Object... messageArguments) {

if (logger.isTraceEnabled()) {
logger.withCorrelationId(infoProvider.getCorrelationId()).trace(message, messageArguments);
logger.withCorrelationId(correlationId).trace(message, messageArguments);
}
}


@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
Expand All @@ -184,7 +199,7 @@ public boolean equals(@Nullable final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
final DefaultMuteableConnectionLogger that = (DefaultMuteableConnectionLogger) o;
final var that = (DefaultMuteableConnectionLogger) o;
return active == that.active &&
Objects.equals(connectionId, that.connectionId) &&
Objects.equals(delegate, that.delegate);
Expand Down

0 comments on commit 0fb1522

Please sign in to comment.