From 98fffd6e4ed8989080959b312b0abc3d63f68e16 Mon Sep 17 00:00:00 2001 From: Julien Ruaux Date: Mon, 17 Jul 2023 21:47:41 -0700 Subject: [PATCH] feat: Added `--log` option for specific log levels --- .../main/java/com/redis/riot/cli/Main.java | 3 ++ .../redis/riot/cli/common/LoggingOptions.java | 36 ++++++------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/plugins/riot/src/main/java/com/redis/riot/cli/Main.java b/plugins/riot/src/main/java/com/redis/riot/cli/Main.java index a6f80b827..ea4e7d9d8 100644 --- a/plugins/riot/src/main/java/com/redis/riot/cli/Main.java +++ b/plugins/riot/src/main/java/com/redis/riot/cli/Main.java @@ -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; @@ -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())); } } diff --git a/plugins/riot/src/main/java/com/redis/riot/cli/common/LoggingOptions.java b/plugins/riot/src/main/java/com/redis/riot/cli/common/LoggingOptions.java index 440460a15..8cbd838e0 100644 --- a/plugins/riot/src/main/java/com/redis/riot/cli/common/LoggingOptions.java +++ b/plugins/riot/src/main/java/com/redis/riot/cli/common/LoggingOptions.java @@ -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; @@ -23,7 +25,6 @@ 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 = ""; @@ -31,8 +32,9 @@ public class LoggingOptions { 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 = "") + private Map logs = new HashMap<>(); private static LoggingOptions getTopLevelCommandLoggingMixin(CommandSpec commandSpec) { return ((Main) commandSpec.root().userObject()).getLoggingOptions(); @@ -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() { @@ -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(); } } \ No newline at end of file