From 154dec996fc54d6db089be461ca50bcd75209f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Magalh=C3=A3es?= Date: Tue, 24 Sep 2024 12:23:35 +0100 Subject: [PATCH] feat(console monitor): Log level can be set via program arg (#4476) * Enable log level configuration via program argument * use default log level in ExtensionLoaderTest * ConsoleMonitor can set log from config value * Adjustments * checkstyle * fixed setting precedence * Removed changes to BaseRuntime * Change behavior when wrong level is provided. * Removed equals and hashcode from ConsoleMonitor * Removed unused bits. * Removed other bits. * nits * suggestion Co-authored-by: Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> * nits * It wasn't a nit after all * Changed to MethodSource * Revert "Changed to MethodSource" This reverts commit bacc49a1aef7aa3fb5dd90aedf38ed8b798fd3f3. --------- Co-authored-by: Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> --- .../edc/boot/system/ExtensionLoader.java | 31 ++++++++++- .../edc/boot/system/ExtensionLoaderTest.java | 55 +++++++++++++++++++ .../edc/spi/monitor/ConsoleMonitor.java | 21 ++++--- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/core/common/boot/src/main/java/org/eclipse/edc/boot/system/ExtensionLoader.java b/core/common/boot/src/main/java/org/eclipse/edc/boot/system/ExtensionLoader.java index 1cb22adbf5a..c32cc0b80cd 100644 --- a/core/common/boot/src/main/java/org/eclipse/edc/boot/system/ExtensionLoader.java +++ b/core/common/boot/src/main/java/org/eclipse/edc/boot/system/ExtensionLoader.java @@ -23,8 +23,10 @@ import org.eclipse.edc.boot.system.injection.ProviderMethod; import org.eclipse.edc.boot.system.injection.ProviderMethodScanner; import org.eclipse.edc.boot.system.injection.lifecycle.ExtensionLifecycleManager; +import org.eclipse.edc.spi.EdcException; import org.eclipse.edc.spi.monitor.ConsoleMonitor; import org.eclipse.edc.spi.monitor.Monitor; +import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.system.MonitorExtension; import org.eclipse.edc.spi.system.ServiceExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; @@ -37,6 +39,7 @@ import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; +import java.util.stream.Stream; public class ExtensionLoader { @@ -89,7 +92,11 @@ private static Supplier getDefaultProviderInvoker(ServiceExtensionContex static @NotNull Monitor loadMonitor(List availableMonitors, String... programArgs) { if (availableMonitors.isEmpty()) { - return new ConsoleMonitor(!Set.of(programArgs).contains("--no-color")); + var parseResult = parseLogLevel(programArgs); + if (parseResult.failed()) { + throw new EdcException(parseResult.getFailureDetail()); + } + return new ConsoleMonitor((ConsoleMonitor.Level) parseResult.getContent(), !Set.of(programArgs).contains(ConsoleMonitor.COLOR_PROG_ARG)); } if (availableMonitors.size() > 1) { @@ -127,4 +134,26 @@ public List loadExtensions(Class type, boolean required) { return serviceLocator.loadImplementors(type, required); } + /** + * Parses the ConsoleMonitor log level from the program args. If no log level is provided, defaults to Level default. + */ + private static Result parseLogLevel(String[] programArgs) { + return Stream.of(programArgs) + .filter(arg -> arg.startsWith(ConsoleMonitor.LEVEL_PROG_ARG)) + .map(arg -> { + var validValueMessage = String.format("Valid values for the console level are %s", Stream.of(ConsoleMonitor.Level.values()).toList()); + var splitArgs = arg.split("="); + if (splitArgs.length != 2) { + return Result.failure(String.format("Value missing for the --log-level argument. %s", validValueMessage)); + } + try { + return Result.success(ConsoleMonitor.Level.valueOf(splitArgs[1].toUpperCase())); + } catch (IllegalArgumentException e) { + return Result.failure(String.format("Invalid value \"%s\" for the --log-level argument. %s", splitArgs[1], validValueMessage)); + } + }) + .findFirst() + .orElse(Result.success(ConsoleMonitor.Level.getDefaultLevel())); + } + } diff --git a/core/common/boot/src/test/java/org/eclipse/edc/boot/system/ExtensionLoaderTest.java b/core/common/boot/src/test/java/org/eclipse/edc/boot/system/ExtensionLoaderTest.java index 8ff5551b627..c25b0918ee9 100644 --- a/core/common/boot/src/test/java/org/eclipse/edc/boot/system/ExtensionLoaderTest.java +++ b/core/common/boot/src/test/java/org/eclipse/edc/boot/system/ExtensionLoaderTest.java @@ -37,10 +37,16 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import org.mockito.Mockito; import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; @@ -98,6 +104,24 @@ void loadMonitor_whenNoMonitorExtension() { assertTrue(monitor instanceof ConsoleMonitor); } + @ParameterizedTest + @ArgumentsSource(LogLevelVariantArgsProvider.class) + void loadMonitor_programArgsSetConsoleMonitorLogLevel(String programArgs, ConsoleMonitor.Level level) { + + var monitor = ExtensionLoader.loadMonitor(new ArrayList<>(), programArgs); + + assertThat(monitor).extracting("level").isEqualTo(level); + } + + @ParameterizedTest + @ArgumentsSource(LogLevelWrongArgProvider.class) + void loadMonitor_consoleMonitorDefaultLogLevelWhenWrongArgs(String programArgs, String expectedMessage) { + assertThatThrownBy(() -> ExtensionLoader.loadMonitor(new ArrayList<>(), programArgs)) + .isInstanceOf(EdcException.class) + .hasMessageContaining(expectedMessage); + + } + @Test void selectOpenTelemetryImpl_whenNoOpenTelemetry() { var openTelemetry = ExtensionLoader.selectOpenTelemetryImpl(emptyList()); @@ -436,6 +460,37 @@ private static class SomeObject { private static class AnotherObject { } + + private static class LogLevelWrongArgProvider implements ArgumentsProvider { + @Override + public Stream provideArguments(ExtensionContext context) throws Exception { + return Stream.of( + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=INF", "Invalid value \"INF\" for the --log-level argument."), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=", "Value missing for the --log-level argument."), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "= INFO", "Invalid value \" INFO\" for the --log-level argument."), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + " INFO", "Value missing for the --log-level argument.") + + + ); + } + } + + private static class LogLevelVariantArgsProvider implements ArgumentsProvider { + @Override + public Stream provideArguments(ExtensionContext context) throws Exception { + return Stream.of( + Arguments.of("", ConsoleMonitor.Level.DEBUG), //default case + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=INFO", ConsoleMonitor.Level.INFO), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=DEBUG", ConsoleMonitor.Level.DEBUG), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=SEVERE", ConsoleMonitor.Level.SEVERE), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=WARNING", ConsoleMonitor.Level.WARNING), + Arguments.of(ConsoleMonitor.LEVEL_PROG_ARG + "=warning", ConsoleMonitor.Level.WARNING) + + + ); + } + } + @BaseExtension private static class TestCoreExtension implements ServiceExtension { diff --git a/spi/common/boot-spi/src/main/java/org/eclipse/edc/spi/monitor/ConsoleMonitor.java b/spi/common/boot-spi/src/main/java/org/eclipse/edc/spi/monitor/ConsoleMonitor.java index 2187e2b1b25..52bb749135d 100644 --- a/spi/common/boot-spi/src/main/java/org/eclipse/edc/spi/monitor/ConsoleMonitor.java +++ b/spi/common/boot-spi/src/main/java/org/eclipse/edc/spi/monitor/ConsoleMonitor.java @@ -30,21 +30,21 @@ public class ConsoleMonitor implements Monitor { private static final String INFO = "INFO"; private static final String DEBUG = "DEBUG"; + public static final String LEVEL_PROG_ARG = "--log-level"; + public static final String COLOR_PROG_ARG = "--no-color"; + + private final boolean useColor; - private final Level level; + private Level level; private final String prefix; public ConsoleMonitor() { - this(true); - } - - public ConsoleMonitor(boolean useColor) { - this(null, Level.DEBUG, useColor); + this(Level.getDefaultLevel(), true); } - public ConsoleMonitor(@Nullable String runtimeName, Level level) { - this(runtimeName, level, true); + public ConsoleMonitor(Level level, boolean useColor) { + this(null, level, useColor); } public ConsoleMonitor(@Nullable String runtimeName, Level level, boolean useColor) { @@ -117,5 +117,10 @@ public enum Level { Level(int value) { this.value = value; } + + public static Level getDefaultLevel() { + return Level.DEBUG; + } + } }