diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2021-08-29 08:34:11 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2021-08-29 08:34:11 +0200 |
commit | 82d7e44344cf5598bd2e93ae846ade8b5e7248ad (patch) | |
tree | f795b52b7b4a2d0f2e267d9a14942c24df65096a /runtime/src/main | |
parent | 747bdeaec0b48404d8a54d03eb1b9bd900c136e1 (diff) |
Add ParameterProvider.
Change-Id: Iae30648eb41761831840de0ce5617072e69928a4
Diffstat (limited to 'runtime/src/main')
3 files changed, 32 insertions, 1 deletions
diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java index d2e5562..c6e177c 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java @@ -3,6 +3,7 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,12 +29,26 @@ public class Formatter extends ExtFormatter { private static final String ERROR_EVENT_TYPE = "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"; + private final List<ParameterProvider> parameterProviders; + + public Formatter(Collection<ParameterProvider> parameterProviders) { + this.parameterProviders = List.copyOf(parameterProviders); + } + @Override public String format(ExtLogRecord logRecord) { var message = formatMessageWithStackTrace(logRecord); List<StructuredParameter> parameters = new ArrayList<>(); Map<String, String> labels = new HashMap<>(); + + for (var parameterProvider : parameterProviders) { + var parameter = parameterProvider.get(); + if (parameter != null) { + parameters.add(parameter); + } + } + if (logRecord.getParameters() != null) { for (var parameter : logRecord.getParameters()) { if (parameter instanceof StructuredParameter) { diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java index db2c2e9..ace4a28 100644 --- a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/GoogleCloudJsonLoggingRecorder.java @@ -1,12 +1,16 @@ package eu.mulk.quarkus.googlecloud.jsonlogging; +import io.quarkus.arc.Arc; import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; import java.util.Optional; +import java.util.stream.Collectors; @Recorder public class GoogleCloudJsonLoggingRecorder { public RuntimeValue<Optional<java.util.logging.Formatter>> initialize() { - return new RuntimeValue<>(Optional.of(new Formatter())); + var parameterProviders = + Arc.container().select(ParameterProvider.class).stream().collect(Collectors.toList()); + return new RuntimeValue<>(Optional.of(new Formatter(parameterProviders))); } } diff --git a/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java new file mode 100644 index 0000000..fb212a3 --- /dev/null +++ b/runtime/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/ParameterProvider.java @@ -0,0 +1,12 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging; + +/** + * A user-supplied provider for {@link StructuredParameter}s. + * + * <p>Any CDI beans registered under this class are applied to each log entry that is logged. + */ +public interface ParameterProvider { + + /** Provides a {@link StructuredParameter} to add to each log entry that is logged. */ + StructuredParameter get(); +} |