Skip to content

Commit

Permalink
feat(console monitor): Log level can be set via program arg (#4476)
Browse files Browse the repository at this point in the history
* 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 bacc49a.

---------

Co-authored-by: Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com>
  • Loading branch information
rafaelmag110 and paullatzelsperger authored Sep 24, 2024
1 parent a2000fd commit 154dec9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -89,7 +92,11 @@ private static Supplier<Object> getDefaultProviderInvoker(ServiceExtensionContex

static @NotNull Monitor loadMonitor(List<MonitorExtension> 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) {
Expand Down Expand Up @@ -127,4 +134,26 @@ public <T> List<T> loadExtensions(Class<T> 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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -436,6 +460,37 @@ private static class SomeObject {
private static class AnotherObject {
}


private static class LogLevelWrongArgProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> 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<? extends Arguments> 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -117,5 +117,10 @@ public enum Level {
Level(int value) {
this.value = value;
}

public static Level getDefaultLevel() {
return Level.DEBUG;
}

}
}

0 comments on commit 154dec9

Please sign in to comment.