Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Jan 19, 2024
1 parent 3372c1f commit 1270443
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 86 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/io/jstach/rainbowgum/KeyValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ default void putAll(Map<String, String> m) {
* @return key values
*/
public static MutableKeyValues of(Map<String, String> m) {
var kvs = of(m.size());
int size = m.size();
var kvs = of(size == 0 ? 4 : size);
kvs.putAll(m);
return kvs;
}
Expand Down Expand Up @@ -350,6 +351,9 @@ public int size() {
}

private int _next(int index) {
if (size == 0) {
return -1;
}
var limit = threshold - 1;
if (index >= limit) {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static String validateName(String name) {
final class DefaultAppenderRegistry implements LogAppenderRegistry {

/*
* The shit in here is a mess because auto configuration of appenders based on
* TODO The shit in here is a mess because auto configuration of appenders based on
* properties is complicated particularly because we want to support Spring Boots
* configuration OOB.
*/
Expand Down
81 changes: 59 additions & 22 deletions core/src/main/java/io/jstach/rainbowgum/LogConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.jstach.rainbowgum;

import static io.jstach.rainbowgum.spi.RainbowGumServiceProvider.findProviders;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

Expand All @@ -11,6 +16,9 @@
import io.jstach.rainbowgum.LevelResolver.LevelConfig;
import io.jstach.rainbowgum.LogConfig.ChangePublisher;
import io.jstach.rainbowgum.LogProperties.PropertyGetter;
import io.jstach.rainbowgum.spi.RainbowGumServiceProvider;
import io.jstach.rainbowgum.spi.RainbowGumServiceProvider.Configurator;
import io.jstach.rainbowgum.spi.RainbowGumServiceProvider.PropertiesProvider;

/**
* The configuration of a RainbowGum. In some other logging implementations this is called
Expand Down Expand Up @@ -65,24 +73,6 @@ default LogOutput output(URI uri, String name) throws IOException {
return outputRegistry().output(uri, name, properties());
}

/**
* Creates default log config backed by system properties.
* @return config.
*/
public static LogConfig of() {
return LogConfig.of(ServiceRegistry.of(), LogProperties.StandardProperties.SYSTEM_PROPERTIES);
}

/**
* Creates config.
* @param registry service registry.
* @param properties properties.
* @return config.
*/
public static LogConfig of(ServiceRegistry registry, LogProperties properties) {
return new DefaultLogConfig(registry, properties);
}

/**
* Creates a builder for making LogConfig.
* @return builder.
Expand Down Expand Up @@ -172,6 +162,10 @@ public static final class Builder {

private @Nullable LogProperties logProperties;

private @Nullable ServiceLoader<RainbowGumServiceProvider> serviceLoader;

private final List<RainbowGumServiceProvider.Configurator> configurators = new ArrayList<>();

/**
* Default constructor
*/
Expand All @@ -183,7 +177,7 @@ private Builder() {
* @param logProperties log properties.
* @return this.
*/
public Builder logProperties(LogProperties logProperties) {
public Builder properties(LogProperties logProperties) {
this.logProperties = logProperties;
return this;
}
Expand All @@ -199,19 +193,62 @@ public Builder serviceRegistry(ServiceRegistry serviceRegistry) {
}

/**
* Builds LogConfig using defaults on missing properties.
* Add to configure LogConfig and ServiceRegistry.
* @param configurator will run on build.
* @return this.
*/
public Builder configurator(RainbowGumServiceProvider.Configurator configurator) {
configurators.add(configurator);
return this;
}

/**
* Sets the service loader to use for loading components that were not set.
* @param serviceLoader loader to use for missing components.
* @return this.
*/
public Builder serviceLoader(ServiceLoader<RainbowGumServiceProvider> serviceLoader) {
this.serviceLoader = serviceLoader;
return this;
}

/**
* Builds LogConfig which will use the {@link ServiceLoader} if set to load
* missing components and if not set will use static defaults.
* @return log config
*/
public LogConfig build() {
ServiceRegistry serviceRegistry = this.serviceRegistry;
LogProperties logProperties = this.logProperties;
var serviceLoader = this.serviceLoader;
var configurators = this.configurators;
if (serviceRegistry == null) {
serviceRegistry = ServiceRegistry.of();
}
if (logProperties == null) {
logProperties = LogProperties.StandardProperties.SYSTEM_PROPERTIES;
if (serviceLoader != null) {
logProperties = provideProperties(serviceRegistry, serviceLoader);
}
else {
logProperties = LogProperties.StandardProperties.SYSTEM_PROPERTIES;
}
}
var config = new DefaultLogConfig(serviceRegistry, logProperties);
if (configurators.isEmpty() && serviceLoader != null) {
configurators = findProviders(serviceLoader, Configurator.class).toList();
}
return LogConfig.of(serviceRegistry, logProperties);
if (!configurators.isEmpty()) {
RainbowGumServiceProvider.Configurator.runConfigurators(configurators.stream(), config);
}
return config;
}

private static LogProperties provideProperties(ServiceRegistry registry,
ServiceLoader<RainbowGumServiceProvider> loader) {
List<LogProperties> props = findProviders(loader, PropertiesProvider.class)
.flatMap(s -> s.provideProperties(registry).stream())
.toList();
return LogProperties.of(props, LogProperties.StandardProperties.SYSTEM_PROPERTIES);
}

}
Expand Down
11 changes: 5 additions & 6 deletions core/src/main/java/io/jstach/rainbowgum/LogEncoderRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ public void register(String name, LogEncoder encoder) {
@Override
public LogEncoder provide(URI uri, String name, LogProperties properties) {
String scheme = uri.getScheme();
String path = uri.getPath();

if (scheme == null && path != null) {
return _encoder(uri, name, path);
if (scheme == null) {
throw new IllegalStateException("Encoder reference needs a URI with scheme. "
+ "For example 'gelf' is not valid but 'gelf:///' is.");
}
else if (scheme.equals("encoder") || scheme.equals("name")) {
if (scheme.equals("name")) {
String _name = uri.getHost();
if (_name == null) {
_name = name;
Expand All @@ -85,7 +84,7 @@ else if (scheme.equals("encoder") || scheme.equals("name")) {

private LogEncoder _encoder(URI uri, String name, String resolvedName) {
return encoder(resolvedName).orElseThrow(() -> new NoSuchElementException(
"No encoder found. resolved = " + resolvedName + " name= " + name + ", uri=" + uri));
"No encoder found. resolved='" + resolvedName + "', name='" + name + "', uri='" + uri + "'"));
}

@Override
Expand Down
34 changes: 30 additions & 4 deletions core/src/main/java/io/jstach/rainbowgum/LogProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public interface LogProperties {
* @apiNote the reason empty map is not returned for missing key is that it creates
* ambiguity so null is returned when key is missing.
*/
default Map<String, String> mapOrNull(String key) {
default @Nullable Map<String, String> mapOrNull(String key) {
String s = valueOrNull(key);
if (s == null) {
return null;
Expand Down Expand Up @@ -209,6 +209,8 @@ public final static class Builder {

private Function<String, String> function;

private Function<String, String> renameKey = s -> s;

private Builder() {
}

Expand Down Expand Up @@ -243,6 +245,16 @@ public Builder function(Function<String, String> function) {
return this;
}

/**
* Renames the key before it accesses function.
* @param renameKey rename key function.
* @return this.
*/
public Builder renameKey(Function<String, String> renameKey) {
this.renameKey = renameKey;
return this;
}

/**
* Parses a string as {@link Properties}.
* @param properties properties as a string.
Expand Down Expand Up @@ -272,15 +284,22 @@ public LogProperties build() {
if (function == null) {
throw new IllegalStateException("function is was not set");
}
return new DefaultLogProperties(function, description, order);
return new DefaultLogProperties(function, description, renameKey, order);
}

private record DefaultLogProperties(Function<String, String> func, String description,
private record DefaultLogProperties( //
Function<String, String> func, //
String description, Function<String, String> renameKey, //
int order) implements LogProperties {

@Override
public @Nullable String valueOrNull(String key) {
return func.apply(key);
return func.apply(renameKey.apply(key));
}

@Override
public String description(String key) {
return renameKey.apply(key);
}

}
Expand Down Expand Up @@ -315,6 +334,13 @@ Function<String, String> parse(String content) {

}

// /**
// * A log properties that can list all keys available.
// */
// public interface ListableProperties extends LogProperties {
// public Set<String> keys();
// }

/**
* LogProperties builder.
* @return builder.
Expand Down
31 changes: 28 additions & 3 deletions core/src/main/java/io/jstach/rainbowgum/RainbowGum.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Optional<RainbowGum> provide(LogConfig config) {
}
*/
//@formatter:on
public interface RainbowGum extends AutoCloseable {
public sealed interface RainbowGum extends AutoCloseable, LogEventLogger {

/**
* Gets the currently statically bound RainbowGum.
Expand Down Expand Up @@ -115,6 +115,18 @@ default void close() {
router().close();
}

/**
* This append call is mainly for testing as it does not avoid making events that do
* not need to be made if no logging needs to be done. {@inheritDoc}
*/
@Override
default void log(LogEvent event) {
var r = router().route(event.loggerName(), event.level());
if (r.isEnabled()) {
r.log(event);
}
}

/**
* Use to build a custom {@link RainbowGum} which will use the {@link LogConfig}
* provided by the service loader.
Expand All @@ -127,7 +139,7 @@ public static Builder builder() {
}

/**
* Use to build a custom {@link RainbowGum}.
* Use to build a custom {@link RainbowGum} with supplied config.
* @param config the config
* @return builder.
* @see #builder()
Expand All @@ -136,14 +148,27 @@ public static Builder builder(LogConfig config) {
return new Builder(config);
}

/**
* Use to build a custom {@link RainbowGum} with supplied config.
* @param config consumer that has first argument as config builder.
* @return builder.
* @see #builder()
* @apiNote this method is for ergonomic fluent reasons.
*/
public static Builder builder(Consumer<? super LogConfig.Builder> config) {
var b = LogConfig.builder();
config.accept(b);
return builder(b.build());
}

/**
* RainbowGum Builder.
*/
public class Builder {

private final LogConfig config;

private List<Router> routes = new ArrayList<>();
private final List<Router> routes = new ArrayList<>();

private Builder(LogConfig config) {
this.config = config;
Expand Down
Loading

0 comments on commit 1270443

Please sign in to comment.