Skip to content

Commit

Permalink
+ renamed LogWriterType to TypedLogWriterFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Jun 20, 2023
1 parent 1a13192 commit 92d505c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 68 deletions.
13 changes: 1 addition & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-engine</artifactId>
<version>11.0.7</version>
<version>11.0.8</version>
<packaging>jar</packaging>
<name>elf4j-engine</name>
<description>A stand-alone Java log engine implementing the ELF4J (Easy Logging Facade for Java) API</description>
Expand Down Expand Up @@ -119,17 +119,6 @@
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility-proxy</artifactId>
<version>3.1.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/elf4j/engine/service/writer/ConseqWriterGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ private ConseqWriterGroup(List<LogWriter> writers, ConseqExecutor conseqExecutor
*/
@NonNull
public static ConseqWriterGroup from(LogServiceConfiguration logServiceConfiguration) {
List<LogWriterType> logWriterTypes = new ArrayList<>(getLogWriterTypes(logServiceConfiguration));
if (logWriterTypes.isEmpty()) {
logWriterTypes.add(new StandardStreamsWriter.StandardStreamsWriterType());
List<TypedLogWriterFactory> typedLogWriterFactories =
new ArrayList<>(getTypedLogWriterFactories(logServiceConfiguration));
if (typedLogWriterFactories.isEmpty()) {
typedLogWriterFactories.add(new StandardStreamsWriter.StandardStreamsWriterFactory());
}
List<LogWriter> logWriters = logWriterTypes.stream()
List<LogWriter> logWriters = typedLogWriterFactories.stream()
.flatMap(t -> t.getLogWriters(logServiceConfiguration).stream())
.collect(Collectors.toList());
IeLogger.INFO.log("{} service writer(s): {}", logWriters.size(), logWriters);
Expand All @@ -95,14 +96,14 @@ private static int getConcurrency(Properties properties) {
return concurrency;
}

private static List<LogWriterType> getLogWriterTypes(LogServiceConfiguration logServiceConfiguration) {
private static List<TypedLogWriterFactory> getTypedLogWriterFactories(@NonNull LogServiceConfiguration logServiceConfiguration) {
String writerTypes = logServiceConfiguration.getProperties().getProperty("writer.types");
if (writerTypes == null) {
return Collections.emptyList();
}
return Arrays.stream(writerTypes.split(",")).map(String::trim).map(fqcn -> {
try {
return (LogWriterType) Class.forName(fqcn).getDeclaredConstructor().newInstance();
return (TypedLogWriterFactory) Class.forName(fqcn).getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException | ClassNotFoundException e) {
throw new IllegalArgumentException(fqcn, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package elf4j.engine.service.writer;

import lombok.NonNull;
import lombok.ToString;

import java.io.*;
Expand All @@ -40,13 +41,19 @@ public class FileStreamStandardOutput implements StandardOutput {
private final OutputStream stderr = new FileOutputStream(FileDescriptor.err);
private final Lock lock = new ReentrantLock();

private static void write(byte[] bytes, @NonNull OutputStream outputStream) {
try {
outputStream.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void out(byte[] bytes) {
lock.lock();
try {
stdout.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
write(bytes, stdout);
} finally {
lock.unlock();
}
Expand All @@ -56,9 +63,7 @@ public void out(byte[] bytes) {
public void err(byte[] bytes) {
lock.lock();
try {
stderr.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
write(bytes, stderr);
} finally {
lock.unlock();
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/elf4j/engine/service/writer/LogWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

import elf4j.Level;
import elf4j.engine.service.LogEvent;
import elf4j.engine.service.configuration.LogServiceConfiguration;

import javax.annotation.concurrent.ThreadSafe;
import java.util.List;

/**
* Implementation should be thread-safe
Expand All @@ -46,4 +48,16 @@ public interface LogWriter extends PerformanceSensitive {
* the log data entry to write out
*/
void write(LogEvent logEvent);

/**
*
*/
interface TypedLogWriterFactory {
/**
* @param logServiceConfiguration
* entire configuration
* @return all log writers of the enclosing writer type from the given configuration
*/
List<LogWriter> getLogWriters(LogServiceConfiguration logServiceConfiguration);
}
}
42 changes: 0 additions & 42 deletions src/main/java/elf4j/engine/service/writer/LogWriterType.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ enum OutStreamType {
/**
*
*/
public static class StandardStreamsWriterType implements LogWriterType {
public static class StandardStreamsWriterFactory implements TypedLogWriterFactory {
private static StandardStreamsWriter getDefaultWriter(@NonNull LogServiceConfiguration logServiceConfiguration) {
Properties properties = logServiceConfiguration.getProperties();
return StandardStreamsWriter.builder()
Expand Down Expand Up @@ -140,4 +140,4 @@ public List<LogWriter> getLogWriters(LogServiceConfiguration logServiceConfigura
.collect(Collectors.toList());
}
}
}
}

0 comments on commit 92d505c

Please sign in to comment.