Skip to content

Commit

Permalink
Add output registry for custom outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Nov 7, 2023
1 parent 25980f7 commit db2ddb7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 51 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/io/jstach/rainbowgum/LogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* The configuration of a RainbowGum. In some other logging implementations this is called
* "context".
*/
public interface LogConfig {
public sealed interface LogConfig {

/**
* String kv properties.
Expand Down Expand Up @@ -50,8 +50,8 @@ default <T> PropertyValue<T> get(Property<T> property) {
* Output provider that uses URI to find output.
* @return output provider.
*/
default LogOutputProvider outputProvider() {
return LogOutputProvider.of();
default LogOutputRegistry outputRegistry() {
return LogOutputRegistry.of();
}

/**
Expand All @@ -61,7 +61,7 @@ default LogOutputProvider outputProvider() {
* @throws IOException if output fails fast
*/
default LogOutput output(URI uri) throws IOException {
return outputProvider().output(uri);
return outputRegistry().output(uri);
}

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ public boolean isEnabled(String loggerName) {

}

class DefaultLogConfig implements LogConfig {
final class DefaultLogConfig implements LogConfig {

private final ServiceRegistry registry;

Expand Down
46 changes: 1 addition & 45 deletions core/src/main/java/io/jstach/rainbowgum/LogOutputProvider.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package io.jstach.rainbowgum;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
* Finds output based on URI.
*/
public interface LogOutputProvider extends AutoCloseable {
public interface LogOutputProvider {

/**
* Loads an output from a URI.
Expand All @@ -20,44 +16,4 @@ public interface LogOutputProvider extends AutoCloseable {
*/
LogOutput output(URI uri) throws IOException;

default void close() {
}

/**
* Default output provider.
* @return default output provider.
*/
public static LogOutputProvider of() {
return DefaultOutputProvider.INSTANCE;
}

}

enum DefaultOutputProvider implements LogOutputProvider {

INSTANCE;

@Override
public LogOutput output(URI uri) throws IOException {
String scheme = uri.getScheme();
String path = uri.getPath();
if (scheme == null && path != null) {
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(path);
return LogOutput.of(uri, fos.getChannel());
}
else if (LogOutput.STDOUT_SCHEME.equals(scheme)) {
return LogOutput.ofStandardOut();
}
else if (LogOutput.STDERR_SCHEME.equals(scheme)) {
return LogOutput.ofStandardErr();
}
else {
var p = Paths.get(uri);
var channel = FileChannel.open(p, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
channel.close();
return LogOutput.of(uri, channel);
}
}

}
70 changes: 70 additions & 0 deletions core/src/main/java/io/jstach/rainbowgum/LogOutputRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.jstach.rainbowgum;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Register output providers by URI scheme.
*/
public interface LogOutputRegistry extends LogOutputProvider {

/**
* Register a provider by {@link URI#getScheme() scheme}.
* @param scheme URI scheme to match for.
* @param provider provider for scheme.
*/
public void put(String scheme, LogOutputProvider provider);

/**
* Default output provider.
* @return default output provider.
*/
public static LogOutputRegistry of() {
return new DefaultOutputRegistry();
}

}

class DefaultOutputRegistry implements LogOutputRegistry {

private final Map<String, LogOutputProvider> providers = new ConcurrentHashMap<>();

@Override
public void put(String scheme, LogOutputProvider provider) {
providers.put(scheme, provider);
}

@Override
public LogOutput output(URI uri) throws IOException {
String scheme = uri.getScheme();
String path = uri.getPath();
LogOutputProvider customProvider;
if (scheme == null && path != null) {
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(path);
return LogOutput.of(uri, fos.getChannel());
}
else if (LogOutput.STDOUT_SCHEME.equals(scheme)) {
return LogOutput.ofStandardOut();
}
else if (LogOutput.STDERR_SCHEME.equals(scheme)) {
return LogOutput.ofStandardErr();
}
else if ((customProvider = providers.get(scheme)) != null) {
return customProvider.output(uri);
}
else {
var p = Paths.get(uri);
var channel = FileChannel.open(p, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
channel.close();
return LogOutput.of(uri, channel);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import org.eclipse.jdt.annotation.Nullable;

import io.jstach.rainbowgum.Defaults;
import io.jstach.rainbowgum.LogConfig;
import io.jstach.rainbowgum.LogOutputProvider;
import io.jstach.rainbowgum.LogProperties;
import io.jstach.rainbowgum.RainbowGum;
import io.jstach.rainbowgum.ServiceRegistry;
Expand Down Expand Up @@ -47,7 +49,11 @@ public non-sealed interface ConfigProvider extends RainbowGumServiceProvider {
}

/**
* Called after {@link LogConfig} has been loaded.
* Called after {@link LogConfig} has been loaded to do various custom initialization
* like registering {@link LogOutputProvider}s.
*
* @see LogConfig#outputRegistry()
* @see Defaults#formatterForOutputType(io.jstach.rainbowgum.LogOutput.OutputType)
*/
public non-sealed interface Initializer extends RainbowGumServiceProvider {

Expand Down

0 comments on commit db2ddb7

Please sign in to comment.