Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(console monitor): Log level can be set via program arg #4476

Merged
merged 17 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ 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"));
return new ConsoleMonitor(parseLogLevel(programArgs), !Set.of(programArgs).contains(ConsoleMonitor.COLOR_PROG_ARG));
}

if (availableMonitors.size() > 1) {
Expand All @@ -112,6 +112,21 @@ private static Supplier<Object> getDefaultProviderInvoker(ServiceExtensionContex
return openTelemetries.isEmpty() ? GlobalOpenTelemetry.get() : openTelemetries.get(0);
}

/**
* Parses the ConsoleMonitor log level from the program args. If no log level is provided, defaults to Level default.
*/
private static ConsoleMonitor.Level parseLogLevel(String[] programArgs) {
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
return Set.of(programArgs).stream().filter(arg -> arg.startsWith(ConsoleMonitor.LEVEL_PROG_ARG)).map(arg -> getLogLevel(arg.split("=")[1])).findFirst().orElse(ConsoleMonitor.Level.getDefaultLevel());
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
}

private static ConsoleMonitor.Level getLogLevel(String level) {
try {
return ConsoleMonitor.Level.valueOf(level);
} catch (IllegalArgumentException e) {
return ConsoleMonitor.Level.getDefaultLevel();
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Loads and orders the service extensions.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.edc.boot.system.ServiceLocatorImpl;
import org.eclipse.edc.boot.system.injection.InjectionContainer;
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.system.MonitorExtension;
import org.eclipse.edc.spi.system.ServiceExtension;
Expand All @@ -35,8 +36,10 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static java.lang.Runtime.getRuntime;
import static java.lang.String.format;
Expand Down Expand Up @@ -85,6 +88,10 @@
public void boot(boolean addShutdownHook) {
monitor = createMonitor();
var config = configurationLoader.loadConfiguration(monitor);
if (monitor instanceof ConsoleMonitor) {
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
programArgs = setLogLevelProgArgFromConfig(config, monitor, programArgs);
monitor = createMonitor();
}
context = createServiceExtensionContext(config);

try {
Expand Down Expand Up @@ -135,6 +142,27 @@
return context;
}

/**
* Sets the ConsoleMonitor level from config, if specified. The config has precedence over the program argument.
*/
@NotNull
protected String[] setLogLevelProgArgFromConfig(Config config, Monitor monitor, String[] programArgs) {
if (monitor instanceof ConsoleMonitor) {
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
var levelConfig = config.getString(ConsoleMonitor.LOG_LEVEL_CONFIG, null);
if (levelConfig != null) {
var logLevelArgs = String.format("%s=%s", ConsoleMonitor.LEVEL_PROG_ARG, levelConfig);
var newProgArgs = new ArrayList<>(Arrays.asList(programArgs));
newProgArgs = (ArrayList<String>) newProgArgs.stream()
.filter(arg -> !arg.startsWith(ConsoleMonitor.LEVEL_PROG_ARG))
.collect(Collectors.toList());
Fixed Show fixed Hide fixed
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
newProgArgs.add(logLevelArgs);
return newProgArgs.toArray(new String[0]);
}
}
return programArgs;

}

/**
* Callback for any error that happened during runtime initialization
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ void loadMonitor_whenNoMonitorExtension() {
assertTrue(monitor instanceof ConsoleMonitor);
}

@Test
void loadMonitor_programArgsSetConsoleMonitorLogLevel() {

var monitor = ExtensionLoader.loadMonitor(new ArrayList<>(), ConsoleMonitor.LEVEL_PROG_ARG + "=INFO");
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved

var expectedMonitor = new ConsoleMonitor(ConsoleMonitor.Level.INFO, true);

assertEquals(monitor, expectedMonitor);
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void loadMonitor_consoleMonitorDefaultLogLevelWhenNoArgs() {

var monitor = ExtensionLoader.loadMonitor(new ArrayList<>());

var expectedMonitor = new ConsoleMonitor(ConsoleMonitor.Level.getDefaultLevel(), true);

assertEquals(monitor, expectedMonitor);
}

@Test
void loadMonitor_consoleMonitorDefaultLogLevelWhenWrongArgs() {
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved

var monitor = ExtensionLoader.loadMonitor(new ArrayList<>(), ConsoleMonitor.LEVEL_PROG_ARG + "=INF");

var expectedMonitor = new ConsoleMonitor(ConsoleMonitor.Level.getDefaultLevel(), true);

assertEquals(monitor, expectedMonitor);
}

@Test
void selectOpenTelemetryImpl_whenNoOpenTelemetry() {
var openTelemetry = ExtensionLoader.selectOpenTelemetryImpl(emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
import org.eclipse.edc.boot.system.ServiceLocator;
import org.eclipse.edc.boot.system.testextensions.BaseExtension;
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.system.ConfigurationExtension;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.health.HealthCheckService;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand Down Expand Up @@ -97,6 +100,21 @@ void shouldLoadConfiguration() {
verify(serviceLocator).loadImplementors(ConfigurationExtension.class, false);
}

@Test
void configCanChangeProgramArgs() {

var config = mock(Config.class);
when(config.getString(ConsoleMonitor.LOG_LEVEL_CONFIG, null)).thenReturn("INFO");

var consoleMonitor = mock(ConsoleMonitor.class);

String[] programArgs = {"--no-color", "--log-level=DEBUG"};

var args = runtime.setLogLevelProgArgFromConfig(config, consoleMonitor, programArgs);

assertThat(args[1]).isEqualTo(String.format("%s=INFO", ConsoleMonitor.LEVEL_PROG_ARG));
}

private static class BaseRuntimeFixture extends BaseRuntime {

private final Monitor monitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.function.Supplier;

/**
Expand All @@ -30,17 +31,22 @@ public class ConsoleMonitor implements Monitor {
private static final String INFO = "INFO";
private static final String DEBUG = "DEBUG";

public static final String LOG_LEVEL_CONFIG = "edc.console-monitor.log-level";
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);
this(Level.getDefaultLevel(), true);
}

public ConsoleMonitor(boolean useColor) {
this(null, Level.DEBUG, useColor);
public ConsoleMonitor(Level level, boolean useColor) {
this(null, level, useColor);
}

public ConsoleMonitor(@Nullable String runtimeName, Level level) {
Expand Down Expand Up @@ -82,6 +88,27 @@ public void debug(Supplier<String> supplier, Throwable... errors) {
output(DEBUG, supplier, errors);
}

@Override
public boolean equals(Object o) {
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ConsoleMonitor that = (ConsoleMonitor) o;
return useColor == that.useColor && level == that.level && Objects.equals(prefix, that.prefix);
}

@Override
public int hashCode() {
return Objects.hash(useColor, level, prefix);
}

public void setLevel(Level level) {
this.level = level;
}

private void output(String level, Supplier<String> supplier, Throwable... errors) {
var time = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
var colorCode = useColor ? getColorCode(level) : "";
Expand Down Expand Up @@ -117,5 +144,10 @@ public enum Level {
Level(int value) {
this.value = value;
}

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

}
}
Loading