Skip to content

Commit

Permalink
Register outputs by name
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Jan 20, 2024
1 parent 1270443 commit 6944b9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
30 changes: 22 additions & 8 deletions core/src/main/java/io/jstach/rainbowgum/LogOutputRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.ConcurrentHashMap;

import io.jstach.rainbowgum.LogOutput.OutputProvider;
import io.jstach.rainbowgum.output.ListLogOutput;

/**
* Register output providers by URI scheme.
Expand All @@ -20,7 +21,12 @@ public sealed interface LogOutputRegistry extends OutputProvider permits Default
/**
* A meta URI scheme to reference outputs registered somewhere else.
*/
public static String NAMED_OUTPUT_SCHEME = "output";
public static String NAMED_OUTPUT_SCHEME = "name";

/**
* The URI scheme for list provider.
*/
public static String LIST_OUTPUT_SCHEME = "list";

/**
* Register a provider by {@link URI#getScheme() scheme}.
Expand Down Expand Up @@ -66,7 +72,12 @@ public void register(String scheme, OutputProvider provider) {

@Override
public void register(String name, LogOutput output) {
outputs.put(name, output);
_register(name, output);
}

private LogOutput _register(String name, LogOutput output) {
outputs.putIfAbsent(name, output);
return output;
}

@Override
Expand All @@ -82,16 +93,16 @@ LogOutput provide(String name, LogProperties properties) throws IOException {
return output(URI.create(name + ":///"), name, properties);
}

@SuppressWarnings("resource")
@Override
public LogOutput output(URI uri, String name, LogProperties properties) throws IOException {
String scheme = uri.getScheme();
String path = uri.getPath();
OutputProvider customProvider;
if (scheme == null && path != null) {
if (name.equals(LogAppender.FILE_APPENDER_NAME)) {
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(path);
return LogOutput.of(uri, fos.getChannel());
return _register(name, LogOutput.of(uri, fos.getChannel()));
}
else {
return provide(name, properties);
Expand All @@ -106,19 +117,22 @@ else if (NAMED_OUTPUT_SCHEME.equals(scheme)) {
return output(_name).orElseThrow(() -> new IOException("Output for name: " + _name + " not found."));
}
else if (LogOutput.STDOUT_SCHEME.equals(scheme)) {
return LogOutput.ofStandardOut();
return _register(name, LogOutput.ofStandardOut());
}
else if (LogOutput.STDERR_SCHEME.equals(scheme)) {
return LogOutput.ofStandardErr();
return _register(name, LogOutput.ofStandardErr());
}
else if (LIST_OUTPUT_SCHEME.equals(scheme)) {
return _register(name, new ListLogOutput());
}
else if ((customProvider = providers.get(scheme)) != null) {
return customProvider.output(uri, name, properties);
return _register(name, customProvider.output(uri, name, properties));
}
else {
var p = Paths.get(uri);
var channel = FileChannel.open(p, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
channel.close();
return LogOutput.of(uri, channel);
return _register(name, LogOutput.of(uri, channel));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ void testFullLoad() throws Exception {
@Test
void testFullLoadUri() throws Exception {
String properties = """
logging.appender.console.encoder=gelf://localhost/?prettyPrint=false
logging.appenders=list
logging.appender.list.output=list:///
logging.appender.list.encoder=gelf://somehost/?prettyPrint=false
""";
LogConfig config = LogConfig.builder()
.properties(LogProperties.builder().fromProperties(properties).build())
Expand All @@ -98,6 +100,12 @@ void testFullLoadUri() throws Exception {
Instant instant = Instant.ofEpochMilli(1);
LogEvent e = LogEvent.of(System.Logger.Level.INFO, "gelf", "hello", null).freeze(instant);
r.log(e);
ListLogOutput output = (ListLogOutput) config.outputRegistry().output("list").orElseThrow();
String actual = output.events().get(0).getValue();
String expected = "{\"host\":\"somehost\",\"short_message\":\"hello\","
+ "\"timestamp\":0.001,\"level\":6,\"_time\":\"1970-01-01T00:00:00.001Z\","
+ "\"_level\":\"INFO\",\"_logger\":\"gelf\",\"_thread_name\":\"main\",\"_thread_id\":\"1\",\"version\":\"1.1\"}\n";
assertEquals(expected, actual);
}
}

Expand Down

0 comments on commit 6944b9a

Please sign in to comment.