Matthias Andreas Benkard | 8090924 | 2022-02-03 20:47:47 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 5 | package eu.mulk.quarkus.googlecloud.jsonlogging.logmanager; |
| 6 | |
| 7 | import eu.mulk.quarkus.googlecloud.jsonlogging.Formatter; |
| 8 | import java.util.logging.Handler; |
| 9 | import org.jboss.logmanager.EmbeddedConfigurator; |
| 10 | import org.jboss.logmanager.handlers.ConsoleHandler; |
| 11 | |
| 12 | /** |
| 13 | * A convenient {@link EmbeddedConfigurator} for JBoss Log Manager. |
| 14 | * |
| 15 | * <p>You can register this class through the {@link java.util.ServiceLoader} mechanism as a |
| 16 | * provider of the {@link EmbeddedConfigurator} interface (under the name of {@code |
| 17 | * org.jboss.logmanager.EmbeddedConfigurator}) to automatically register a {@link ConsoleHandler} |
| 18 | * using {@link Formatter} as the default log output method for the application. |
| 19 | */ |
| 20 | public final class DefaultEmbeddedConfigurator implements EmbeddedConfigurator { |
| 21 | |
| 22 | private final Handler[] rootHandlers; |
| 23 | |
| 24 | /** |
| 25 | * Constructs a JBoss Log Manager configuration that uses {@link Formatter} and {@link |
| 26 | * ConsoleHandler} for log output. |
| 27 | */ |
| 28 | @SuppressWarnings("java:S2095") |
| 29 | public DefaultEmbeddedConfigurator() { |
| 30 | rootHandlers = new Handler[] {createConsoleHandler()}; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Creates a {@link ConsoleHandler} that uses {@link Formatter} for formatting. |
| 35 | * |
| 36 | * @return a preconfigured {@link ConsoleHandler}. |
| 37 | */ |
| 38 | public static ConsoleHandler createConsoleHandler() { |
| 39 | return new DefaultConsoleHandler(); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Handler[] getHandlersOf(String loggerName) { |
| 44 | if (loggerName.isEmpty()) { |
| 45 | return rootHandlers; |
| 46 | } else { |
| 47 | return EmbeddedConfigurator.NO_HANDLERS; |
| 48 | } |
| 49 | } |
| 50 | } |