diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2022-01-15 14:03:41 +0100 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2022-01-15 14:27:26 +0100 |
commit | 93ecfd1e0c1d1e40fb17e580a7a11de35383ef79 (patch) | |
tree | 6265b4bfe75ddab0a566c6d1b32ca1391081fd9b /core/src/main/java | |
parent | 20210245e619658c2459c77223d9abe3c643a882 (diff) |
Load providers registered through the ServiceLoader mechanism.
Change-Id: I392e78b34c8330e9b4c06d57b1423ca552ba6fc1
Diffstat (limited to 'core/src/main/java')
-rw-r--r-- | core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java | 29 | ||||
-rw-r--r-- | core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java | 10 |
2 files changed, 37 insertions, 2 deletions
diff --git a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java index 066f709..61a2dea 100644 --- a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java +++ b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.ServiceLoader; import java.util.logging.Level; import org.jboss.logmanager.ExtFormatter; import org.jboss.logmanager.ExtLogRecord; @@ -35,6 +36,9 @@ public class Formatter extends ExtFormatter { /** * Constructs a {@link Formatter}. * + * <p><strong>Note:</strong> This constructor does not automatically discover providers using the + * {@link ServiceLoader} mechanism. See {@link #load} for this case use. + * * @param parameterProviders the {@link StructuredParameterProvider}s to apply to each log entry. * @param labelProviders the {@link LabelProvider}s to apply to each log entry. */ @@ -45,6 +49,31 @@ public class Formatter extends ExtFormatter { this.labelProviders = List.copyOf(labelProviders); } + /** + * Constructs a {@link Formatter} with parameter and label providers supplied by {@link + * ServiceLoader}. + * + * <p>In addition to the providers supplied as parameters, this factory method loads all {@link + * StructuredParameterProvider}s and {@link LabelProvider}s found through the {@link + * ServiceLoader} mechanism. + * + * @param parameterProviders the {@link StructuredParameterProvider}s to apply to each log entry. + * @param labelProviders the {@link LabelProvider}s to apply to each log entry. + */ + public static Formatter load( + Collection<StructuredParameterProvider> parameterProviders, + Collection<LabelProvider> labelProviders) { + parameterProviders = new ArrayList<>(parameterProviders); + ServiceLoader.load(StructuredParameterProvider.class, Formatter.class.getClassLoader()) + .forEach(parameterProviders::add); + + labelProviders = new ArrayList<>(labelProviders); + ServiceLoader.load(LabelProvider.class, Formatter.class.getClassLoader()) + .forEach(labelProviders::add); + + return new Formatter(parameterProviders, labelProviders); + } + @Override public String format(ExtLogRecord logRecord) { var message = formatMessageWithStackTrace(logRecord); diff --git a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java index 2f4c7ce..3617b8c 100644 --- a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java +++ b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/package-info.java @@ -123,8 +123,14 @@ * parameters for each message that is logged. This can be used to provide contextual information * such as tracing and request IDs stored in thread-local storage. * - * <p>If you are using the Quarkus extension, CDI beans that implement these interfaces are - * automatically detected at build time and passed to the formatter on startup. + * <p><strong>Service provider support:</strong> Providers can be registered using the {@link + * java.util.ServiceLoader} mechanism, in which case {@link + * eu.mulk.quarkus.googlecloud.jsonlogging.Formatter#load} picks them up automatically. + * + * <p><strong>CDI support:</strong> If you are using the Quarkus extension, CDI beans that implement + * one of the provider interfaces are automatically detected at build time and passed to the + * formatter on startup. In addition, providers using the {@link java.util.ServiceLoader} mechanism + * are detected and passed to the formatter as well. * * <p><strong>Example:</strong> * |