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 11 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 Down Expand Up @@ -127,4 +127,21 @@ 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 ConsoleMonitor.Level parseLogLevel(String[] programArgs) {
return Set.of(programArgs).stream()
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
.filter(arg -> arg.startsWith(ConsoleMonitor.LEVEL_PROG_ARG))
.map(arg -> {
try {
return ConsoleMonitor.Level.valueOf(arg.split("=")[1]);
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Illegal Console Monitor log level value: %s. Possible values are DEBUG, INFO, WARNING, SEVERE", arg.split("=")[1]));
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
}
})
.findFirst()
.orElse(ConsoleMonitor.Level.getDefaultLevel());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ 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

assertThat(monitor).extracting("level").isEqualTo(ConsoleMonitor.Level.INFO);
}

@Test
void loadMonitor_consoleMonitorDefaultLogLevelWhenNoArgs() {

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

assertThat(monitor).extracting("level").isEqualTo(ConsoleMonitor.Level.getDefaultLevel());
}

@Test
void loadMonitor_consoleMonitorDefaultLogLevelWhenWrongArgs() {
rafaelmag110 marked this conversation as resolved.
Show resolved Hide resolved
assertThatThrownBy(() -> ExtensionLoader.loadMonitor(new ArrayList<>(), ConsoleMonitor.LEVEL_PROG_ARG + "=INF")).isInstanceOf(IllegalArgumentException.class);
}

@Test
void selectOpenTelemetryImpl_whenNoOpenTelemetry() {
var openTelemetry = ExtensionLoader.selectOpenTelemetryImpl(emptyList());
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;
}

}
}
Loading