Skip to content

Commit

Permalink
feat: Added --log option for specific log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Jul 18, 2023
1 parent a110e70 commit 98fffd6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
3 changes: 3 additions & 0 deletions plugins/riot/src/main/java/com/redis/riot/cli/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.redis.riot.cli;

import java.util.logging.Level;

import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;

Expand Down Expand Up @@ -81,6 +83,7 @@ protected void registerConverters(CommandLine commandLine) {
SpelExpressionParser parser = new SpelExpressionParser();
commandLine.registerConverter(Expression.class, parser::parseExpression);
commandLine.registerConverter(ReadFrom.class, ReadFrom::valueOf);
commandLine.registerConverter(Level.class, s -> Level.parse(s.toUpperCase()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static picocli.CommandLine.Spec.Target.MIXEE;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
Expand All @@ -23,16 +25,16 @@
public class LoggingOptions {

public static final Level DEFAULT_LEVEL = RiotLevel.LIFECYCLE;
private static final Level DEFAULT_DEPENDENCY_LEVEL = Level.SEVERE;
public static final boolean DEFAULT_STACKTRACE = false;
private static final String ROOT_LOGGER = "";

private @Spec(MIXEE) CommandSpec mixee;

private Level level = DEFAULT_LEVEL;
private boolean stacktrace = DEFAULT_STACKTRACE;
@Option(names = "--dep-log-level", description = "Set log level for dependencies", hidden = true)
private Level dependencyLevel = DEFAULT_DEPENDENCY_LEVEL;

@Option(arity = "1..*", names = "--log", description = "Log levels in the form name1=level1 name2=level2", paramLabel = "<n=lvl>")
private Map<String, Level> logs = new HashMap<>();

private static LoggingOptions getTopLevelCommandLoggingMixin(CommandSpec commandSpec) {
return ((Main) commandSpec.root().userObject()).getLoggingOptions();
Expand Down Expand Up @@ -95,24 +97,10 @@ public void configureLoggers() {
activeLogger.addHandler(handler);
Level logLevel = logLevel();
Logger.getLogger(ROOT_LOGGER).setLevel(logLevel);
Logger.getLogger("com.amazonaws").setLevel(dependencyLevel());
Logger.getLogger("io.lettuce").setLevel(dependencyLevel());
Logger.getLogger("org.springframework").setLevel(dependencyLevel());
}

private Level dependencyLevel() {
Level logLevel = logLevel();
if (logLevel == DEFAULT_LEVEL) {
return dependencyLevel;
}
return min(dependencyLevel, logLevel);
}

private Level min(Level level1, Level level2) {
if (level1.intValue() < level2.intValue()) {
return level1;
}
return level2;
Logger.getLogger("com.amazonaws").setLevel(Level.SEVERE);
Logger.getLogger("io.lettuce").setLevel(DEFAULT_LEVEL);
Logger.getLogger("io.netty").setLevel(DEFAULT_LEVEL);
logs.forEach((n, l) -> Logger.getLogger(n).setLevel(l));
}

private Level logLevel() {
Expand All @@ -131,11 +119,7 @@ private boolean isStacktraceEnabled() {
}

private boolean isVerbose() {
return !isGreaterOrEqual(Level.CONFIG);
}

public boolean isGreaterOrEqual(Level level) {
return logLevel().intValue() >= level.intValue();
return logLevel().intValue() < RiotLevel.LIFECYCLE.intValue();
}

}

0 comments on commit 98fffd6

Please sign in to comment.