Skip to content

Commit

Permalink
Log level working
Browse files Browse the repository at this point in the history
Signed-off-by: Václav Muzikář <[email protected]>
  • Loading branch information
vmuzikar committed Nov 18, 2024
1 parent 0406818 commit 5c5d8d9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
35 changes: 31 additions & 4 deletions quarkus/config-api/src/main/java/org/keycloak/config/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
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<T> type;
private final String key;
Expand All @@ -18,7 +19,7 @@ public class Option<T> {
private final List<String> expectedValues;
private final boolean strictExpectedValues;
private final DeprecatedMetadata deprecatedMetadata;
private final boolean hasWildcard;
private Pattern optionNameWildcardPattern;

public Option(Class<T> type, String key, OptionCategory category, boolean hidden, boolean buildTime, String description, Optional<T> defaultValue, List<String> expectedValues, boolean strictExpectedValues, DeprecatedMetadata deprecatedMetadata) {
this.type = type;
Expand All @@ -31,7 +32,14 @@ public Option(Class<T> 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<T> getType() {
Expand Down Expand Up @@ -81,7 +89,26 @@ public Optional<DeprecatedMetadata> 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<String> 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<T> withRuntimeSpecificDefault(T defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,6 +113,23 @@ public static ConfigValue getKcConfigValue(String propertyName) {
return getConfigValue(NS_KEYCLOAK_PREFIX.concat(propertyName));
}

public static Map<String, ConfigValue> 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<String, ConfigValue> 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<String> getOptionalValue(String name) {
return getConfig().getOptionalValue(name, String.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +107,6 @@ public static PropertyMapper<?>[] getMappers() {
.paramLabel("category:level")
.build(),
fromOption(LoggingOptions.LOG_LEVEL_CATEGORY)
.transformer(LoggingPropertyMappers::setCategoryLogLevel)
.paramLabel("level")
.build(),
// Syslog
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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-<category> take precedence
Set<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> option, String to, BooleanSupplier enabled, String enabledWhen,
BiFunction<String, ConfigSourceInterceptorContext, String> mapper,
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public List<PropertyMapper<?>> get(Object key) {
// First check if the requested option matches any wildcard mappers
String strKey = (String) key;
List<PropertyMapper<?>> ret = wildcardMappers.stream()
.filter(m -> m.keyMatchesWildcard(strKey))
.filter(m -> m.matchesWildcardOptionName(strKey))
.toList();
if (!ret.isEmpty()) {
return ret;
Expand Down

0 comments on commit 5c5d8d9

Please sign in to comment.