From ddcce2e8bc1c43e73a4033b4d76b5e06a4a3ad89 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 24 Sep 2023 12:57:37 +0200 Subject: feat(core): Add DefaultConfiguratorFactory for JBoss Log Manager 3.x. Change-Id: I6bf88c9e6c8c4aba303cc1d18e8f18c917dd6b88 --- core/pom.xml | 9 +++ .../logmanager/DefaultConfiguratorFactory.java | 65 ++++++++++++++++++++++ .../logmanager/DefaultEmbeddedConfigurator.java | 4 +- 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultConfiguratorFactory.java (limited to 'core') diff --git a/core/pom.xml b/core/pom.xml index d01555b..18da1a8 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -24,14 +24,23 @@ SPDX-License-Identifier: LGPL-3.0-or-later org.jboss.logmanager jboss-logmanager-embedded + 1.1.1 + + + org.jboss.logmanager + jboss-logmanager + 3.0.2.Final + true io.smallrye.common smallrye-common-constraint + 2.1.0 org.eclipse.parsson parsson + 1.1.2 diff --git a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultConfiguratorFactory.java b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultConfiguratorFactory.java new file mode 100644 index 0000000..2ac5587 --- /dev/null +++ b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultConfiguratorFactory.java @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: © 2023 Matthias Andreas Benkard +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +package eu.mulk.quarkus.googlecloud.jsonlogging.logmanager; + +import eu.mulk.quarkus.googlecloud.jsonlogging.Formatter; +import java.io.InputStream; +import java.util.logging.Handler; +import org.jboss.logmanager.ConfiguratorFactory; +import org.jboss.logmanager.LogContext; +import org.jboss.logmanager.LogContextConfigurator; +import org.jboss.logmanager.handlers.ConsoleHandler; + +/** + * A convenient {@link ConfiguratorFactory} for JBoss Log Manager. + * + *

You can register this class through the {@link java.util.ServiceLoader} mechanism as a + * provider of the {@link ConfiguratorFactory} interface (under the name of {@code + * org.jboss.logmanager.ConfiguratorFactory}) to automatically register a {@link ConsoleHandler} + * using {@link Formatter} as the default log output method for the application. + */ +public final class DefaultConfiguratorFactory implements ConfiguratorFactory { + + private final Handler[] rootHandlers; + private final ConfiguratorFactory upstreamConfiguratorFactory; + + /** + * Constructs a JBoss Log Manager configuration that uses {@link Formatter} and {@link + * ConsoleHandler} for log output. + */ + @SuppressWarnings({"java:S2095", "resource"}) + public DefaultConfiguratorFactory() { + rootHandlers = new Handler[] {createConsoleHandler()}; + upstreamConfiguratorFactory = + new org.jboss.logmanager.configuration.DefaultConfiguratorFactory(); + } + + /** + * Creates a {@link ConsoleHandler} that uses {@link Formatter} for formatting. + * + * @return a preconfigured {@link ConsoleHandler}. + */ + public static ConsoleHandler createConsoleHandler() { + return new DefaultConsoleHandler(); + } + + @Override + public LogContextConfigurator create() { + var upstreamConfigurator = upstreamConfiguratorFactory.create(); + return new LogContextConfigurator() { + @Override + public void configure(LogContext logContext, InputStream inputStream) { + upstreamConfigurator.configure(logContext, inputStream); + var logger = logContext.getLogger(""); + logger.setHandlers(rootHandlers); + } + }; + } + + @Override + public int priority() { + return 50; + } +} diff --git a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultEmbeddedConfigurator.java b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultEmbeddedConfigurator.java index e2ad986..79e9288 100644 --- a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultEmbeddedConfigurator.java +++ b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/logmanager/DefaultEmbeddedConfigurator.java @@ -10,7 +10,7 @@ import org.jboss.logmanager.EmbeddedConfigurator; import org.jboss.logmanager.handlers.ConsoleHandler; /** - * A convenient {@link EmbeddedConfigurator} for JBoss Log Manager. + * A convenient {@link EmbeddedConfigurator} for JBoss Log Manager Embedded (1.1.x and earlier). * *

You can register this class through the {@link java.util.ServiceLoader} mechanism as a * provider of the {@link EmbeddedConfigurator} interface (under the name of {@code @@ -25,7 +25,7 @@ public final class DefaultEmbeddedConfigurator implements EmbeddedConfigurator { * Constructs a JBoss Log Manager configuration that uses {@link Formatter} and {@link * ConsoleHandler} for log output. */ - @SuppressWarnings("java:S2095") + @SuppressWarnings({"java:S2095", "resource"}) public DefaultEmbeddedConfigurator() { rootHandlers = new Handler[] {createConsoleHandler()}; } -- cgit v1.2.3