Skip to content
Prev Previous commit
Next Next commit
Address review concerns
  • Loading branch information
stIncMale committed Oct 19, 2023
commit e09930f00bfe8b010ee7b364e2d4947a7c93a0d2
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.internal.logging;

import com.mongodb.connection.ClusterId;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.lang.Nullable;

import java.util.Collection;
Expand All @@ -28,6 +29,7 @@
import java.util.stream.Stream;

import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
import static java.util.function.Function.identity;

/**
Expand Down Expand Up @@ -60,10 +62,12 @@ public enum Component {
this.value = value;
}

@VisibleForTesting(otherwise = PRIVATE)
public String getValue() {
return value;
}

@VisibleForTesting(otherwise = PRIVATE)
public static Component of(final String value) {
Component result = INDEX.get(value);
return assertNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;

Expand Down Expand Up @@ -87,35 +88,30 @@ public void log(final LogMessage logMessage) {
}
switch (logMessage.getLevel()) {
case DEBUG:
logUnstructured(logMessage, Logger::isDebugEnabled, Logger::debug, Logger::debug);
logUnstructured(logMessage, logger::isDebugEnabled, logger::debug, logger::debug);
break;
case INFO:
logUnstructured(logMessage, Logger::isInfoEnabled, Logger::info, Logger::info);
logUnstructured(logMessage, logger::isInfoEnabled, logger::info, logger::info);
break;
default:
throw new UnsupportedOperationException();
}
}

private void logUnstructured(
private static void logUnstructured(
final LogMessage logMessage,
final Predicate<Logger> loggingEnabled,
final BiConsumer<Logger, String> doLog,
final TriConsumer<Logger, String, Throwable> doLogWithException) {
if (loggingEnabled.test(logger)) {
final Supplier<Boolean> loggingEnabled,
final Consumer<String> doLog,
final BiConsumer<String, Throwable> doLogWithException) {
if (loggingEnabled.get()) {
LogMessage.UnstructuredLogMessage unstructuredLogMessage = logMessage.toUnstructuredLogMessage();
String message = unstructuredLogMessage.interpolate();
Throwable exception = logMessage.getException();
if (exception == null) {
doLog.accept(logger, message);
doLog.accept(message);
} else {
doLogWithException.accept(logger, message, exception);
doLogWithException.accept(message, exception);
}
}
}

@FunctionalInterface
private interface TriConsumer<A, B, C> {
void accept(A a, B b, C c);
}
}