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