From 5c5d8d9d783e531e0b1fb1672d17327a522650d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Muzik=C3=A1=C5=99?= Date: Mon, 18 Nov 2024 17:24:28 +0100 Subject: [PATCH] Log level working MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Muzikář --- .../main/java/org/keycloak/config/Option.java | 35 ++++++++++++++++--- .../runtime/configuration/Configuration.java | 18 ++++++++++ .../mappers/LoggingPropertyMappers.java | 27 +++++++------- .../configuration/mappers/PropertyMapper.java | 10 ++---- .../mappers/PropertyMappers.java | 2 +- 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/quarkus/config-api/src/main/java/org/keycloak/config/Option.java b/quarkus/config-api/src/main/java/org/keycloak/config/Option.java index 944a3919e939..ed237aca7a50 100644 --- a/quarkus/config-api/src/main/java/org/keycloak/config/Option.java +++ b/quarkus/config-api/src/main/java/org/keycloak/config/Option.java @@ -2,11 +2,12 @@ import java.util.List; import java.util.Optional; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class Option { - public static final Pattern WILD_CARD_PATTERN = Pattern.compile("<[-a-zA-Z0-9]+>"); + public static final Pattern WILDCARD_PLACEHOLDER_PATTERN = Pattern.compile("<.+>"); private final Class type; private final String key; @@ -18,7 +19,7 @@ public class Option { private final List expectedValues; private final boolean strictExpectedValues; private final DeprecatedMetadata deprecatedMetadata; - private final boolean hasWildcard; + private Pattern optionNameWildcardPattern; public Option(Class type, String key, OptionCategory category, boolean hidden, boolean buildTime, String description, Optional defaultValue, List expectedValues, boolean strictExpectedValues, DeprecatedMetadata deprecatedMetadata) { this.type = type; @@ -31,7 +32,14 @@ public Option(Class type, String key, OptionCategory category, boolean hidden this.expectedValues = expectedValues; this.strictExpectedValues = strictExpectedValues; this.deprecatedMetadata = deprecatedMetadata; - this.hasWildcard = key != null ? WILD_CARD_PATTERN.matcher(key).matches() : false; + + + if (key != null) { + Matcher matcher = WILDCARD_PLACEHOLDER_PATTERN.matcher(key); + if (matcher.find()) { + this.optionNameWildcardPattern = Pattern.compile(matcher.replaceFirst("([-\\\\\\\\.a-zA-Z0-9]+)")); + } + } } public Class getType() { @@ -81,7 +89,26 @@ public Optional getDeprecatedMetadata() { } public boolean hasWildcard() { - return hasWildcard; + return optionNameWildcardPattern != null; + } + + public boolean matchesWildcardOptionName(String name) { + if (!hasWildcard()) { + throw new IllegalStateException("Option does not have wildcard"); + } + return optionNameWildcardPattern.matcher(name).matches(); + } + + public Optional getWildcardValue(String option) { + if (!hasWildcard()) { + throw new IllegalStateException("Option does not have wildcard"); + } + Matcher matcher = optionNameWildcardPattern.matcher(option); + if (matcher.matches()) { + return Optional.of(matcher.group(1)); + } else { + return Optional.empty(); + } } public Option withRuntimeSpecificDefault(T defaultValue) { diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/Configuration.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/Configuration.java index d196111ee500..cafdf9e9e379 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/Configuration.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/Configuration.java @@ -19,6 +19,7 @@ import static org.keycloak.quarkus.runtime.cli.Picocli.ARG_PREFIX; +import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -112,6 +113,23 @@ public static ConfigValue getKcConfigValue(String propertyName) { return getConfigValue(NS_KEYCLOAK_PREFIX.concat(propertyName)); } + public static Map getKcConfigValues(Option option) { + if (!option.hasWildcard()) { + throw new IllegalArgumentException("Option does not have wildcard"); + } + + // this is not optimal + // TODO find an efficient way to get all values that match the wildcard + Map values = new HashMap<>(); + getPropertyNames().forEach(name -> { + String nameWithoutPrefix = name.startsWith(NS_KEYCLOAK_PREFIX) ? name.substring(NS_KEYCLOAK_PREFIX.length()) : name; + option.getWildcardValue(nameWithoutPrefix) + .ifPresent(s -> values.put(s, getConfigValue(name))); + }); + + return values; + } + public static Optional getOptionalValue(String name) { return getConfig().getOptionalValue(name, String.class); } diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/LoggingPropertyMappers.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/LoggingPropertyMappers.java index e4f80a4f7a70..257a09ff5377 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/LoggingPropertyMappers.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/LoggingPropertyMappers.java @@ -5,12 +5,15 @@ import static org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper.fromOption; import java.io.File; +import java.util.HashSet; import java.util.Locale; +import java.util.Set; import java.util.function.BiFunction; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; +import io.smallrye.config.ConfigValue; import org.jboss.logmanager.LogContext; import org.keycloak.config.LoggingOptions; import org.keycloak.config.Option; @@ -104,7 +107,6 @@ public static PropertyMapper[] getMappers() { .paramLabel("category:level") .build(), fromOption(LoggingOptions.LOG_LEVEL_CATEGORY) - .transformer(LoggingPropertyMappers::setCategoryLogLevel) .paramLabel("level") .build(), // Syslog @@ -191,11 +193,8 @@ private static Level toLevel(String categoryLevel) throws IllegalArgumentExcepti return LogContext.getLogContext().getLevelForName(categoryLevel.toUpperCase(Locale.ROOT)); } - private static void setCategoryLevel(String category, String level, boolean overwrite) { - Logger logger = LogContext.getLogContext().getLogger(category); - if (overwrite || logger.getLevel() == null) { - logger.setLevel(toLevel(level)); - } + private static void setCategoryLevel(String category, String level) { + LogContext.getLogContext().getLogger(category).setLevel(toLevel(level)); } record CategoryLevel(String category, String levelName) {} @@ -225,23 +224,25 @@ private static CategoryLevel validateLogLevel(String level) { private static String resolveLogLevel(String value, ConfigSourceInterceptorContext configSourceInterceptorContext) { String rootLevel = LoggingOptions.DEFAULT_LOG_LEVEL.name(); + // category log levels from log-level- take precedence + Set configuredCategories = new HashSet<>(); + Configuration.getKcConfigValues(LoggingOptions.LOG_LEVEL_CATEGORY).forEach((category, configValue) -> { + setCategoryLevel(category, configValue.getValue()); + configuredCategories.add(category); + }); + for (String level : value.split(",")) { var categoryLevel = validateLogLevel(level); if (categoryLevel.category == null) { rootLevel = categoryLevel.levelName; - } else { - setCategoryLevel(categoryLevel.category, categoryLevel.levelName, false); + } else if (!configuredCategories.contains(categoryLevel.category)) { + setCategoryLevel(categoryLevel.category, categoryLevel.levelName); } } return rootLevel; } - private static String setCategoryLogLevel(String category, ConfigSourceInterceptorContext configSourceInterceptorContext) { - setCategoryLevel(category, level, true); - return level; - } - private static String resolveLogOutput(String value, ConfigSourceInterceptorContext context) { boolean isDefault = LoggingOptions.DEFAULT_CONSOLE_OUTPUT.name().toLowerCase(Locale.ROOT).equals(value); return Boolean.valueOf(!isDefault).toString(); diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java index 8ca76bf0b83f..482f2c69592d 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMapper.java @@ -31,7 +31,6 @@ import java.util.function.BiFunction; import java.util.function.BooleanSupplier; import java.util.function.Consumer; -import java.util.regex.Pattern; import java.util.stream.Stream; import io.smallrye.config.ConfigSourceInterceptorContext; @@ -91,7 +90,6 @@ public ConfigValue getConfigValue(String name, ConfigSourceInterceptorContext co private final String description; private final BooleanSupplier required; private final String requiredWhen; - private final Pattern wildcardPattern; PropertyMapper(Option option, String to, BooleanSupplier enabled, String enabledWhen, BiFunction mapper, @@ -113,10 +111,6 @@ public ConfigValue getConfigValue(String name, ConfigSourceInterceptorContext co this.validator = validator; this.description = description; this.parentMapper = parentMapper; - - String pattern = Pattern.quote(this.to); - pattern = Option.WILD_CARD_PATTERN.matcher(pattern).replaceFirst(Option.WILD_CARD_PATTERN.pattern()); - this.wildcardPattern = Pattern.compile(pattern); } ConfigValue getConfigValue(ConfigSourceInterceptorContext context) { @@ -265,8 +259,8 @@ public boolean hasWildcard() { return option.hasWildcard(); } - public boolean keyMatchesWildcard(String key) { - return wildcardPattern.matcher(key).matches(); + public boolean matchesWildcardOptionName(String name) { + return option.matchesWildcardOptionName(name); } private ConfigValue transformValue(String name, ConfigValue configValue, ConfigSourceInterceptorContext context, boolean parentValue) { diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMappers.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMappers.java index 56f60db0363c..987ca9a5b266 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMappers.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/mappers/PropertyMappers.java @@ -249,7 +249,7 @@ public List> get(Object key) { // First check if the requested option matches any wildcard mappers String strKey = (String) key; List> ret = wildcardMappers.stream() - .filter(m -> m.keyMatchesWildcard(strKey)) + .filter(m -> m.matchesWildcardOptionName(strKey)) .toList(); if (!ret.isEmpty()) { return ret;