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.io.InputStream; |
| 9 | import java.util.Collections; |
| 10 | import org.jboss.logmanager.handlers.ConsoleHandler; |
| 11 | |
| 12 | /** |
| 13 | * A {@link ConsoleHandler} preconfigured with {@link Formatter}. |
| 14 | * |
| 15 | * <p>Useful as a handler for {@link java.util.logging}. |
| 16 | * |
| 17 | * <p>If you have a {@code logging.properties} file (see {@link |
| 18 | * java.util.logging.LogManager#readConfiguration(InputStream)}), you can use this handler by |
| 19 | * setting the following properties: |
| 20 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 21 | * {@snippet lang="properties" : |
Matthias Andreas Benkard | db2440e | 2022-10-23 12:04:13 +0200 | [diff] [blame] | 22 | * handlers = eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConsoleHandler |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 23 | * } |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 24 | * |
| 25 | * <p><strong>Note:</strong> You can use {@code org.slf4j.bridge.SLF4JBridgeHandler} from {@code |
| 26 | * org.slf4j:jul-to-slf4j} instead if you also have {@code org.jboss.slf4j:slf4j-jboss-logmanager} |
| 27 | * on the class path. In comparison to this class, which relies on the relatively efficient {@link |
| 28 | * org.jboss.logmanager.ExtLogRecord#wrap}, routing through SLF4J incurs additional overhead because |
| 29 | * of the necessary conversions between SLF4J's log entry structure and {@link |
| 30 | * java.util.logging.LogRecord}. |
| 31 | * |
| 32 | * <h2>Usage with Spring Boot</h2> |
| 33 | * |
| 34 | * <p>In case you are using Spring Boot, note that in addition to ensuring that {@code |
| 35 | * org.springframework.boot.logging.java.JavaLoggingSystem} is the logging system in use (see |
| 36 | * below), you need to accompany this with an entry in {@code application.properties} that points to |
| 37 | * your {@code logging.properties} file: |
| 38 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 39 | * {@snippet lang="properties" : |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 40 | * logging.config = classpath:logging.properties |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 41 | * } |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 42 | * |
| 43 | * <p>In order to ensure that Spring Boot chooses {@code JavaLoggingSystem} over other |
| 44 | * implementations, make sure that no other logging backends are present on the class path. A simple |
| 45 | * way of doing this is by relying on {@code spring-boot-starter-logging} while excluding Logback: |
| 46 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 47 | * {@snippet lang="xml" : |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 48 | * <dependency> |
| 49 | * <groupId>org.springframework.boot</groupId> |
| 50 | * <artifactId>spring-boot-starter</artifactId> |
| 51 | * <exclusions> |
| 52 | * <exclusion> |
| 53 | * <groupId>ch.qos.logback</groupId> |
| 54 | * <artifactId>logback-classic</artifactId> |
| 55 | * </exclusion> |
| 56 | * </exclusions> |
| 57 | * </dependency> |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 58 | * } |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 59 | * |
| 60 | * <p>You will probably want to include at least {@code org.jboss.slf4j:slf4j-jboss-logmanager} as |
| 61 | * well. In addition, {@code org.slf4j:jcl-over-slf4j}, {@code |
| 62 | * org.jboss.logmanager:log4j-jboss-logmanager}, and {@code |
| 63 | * org.jboss.logmanager:log4j2-jboss-logmanager} may be useful, but are not required. |
| 64 | */ |
| 65 | @SuppressWarnings("java:S110") |
| 66 | public final class DefaultConsoleHandler extends ConsoleHandler { |
| 67 | |
| 68 | /** Constructs console handler with a formatter created by {@link Formatter#load}. */ |
| 69 | public DefaultConsoleHandler() { |
| 70 | super(Formatter.load(Collections.emptyList(), Collections.emptyList())); |
| 71 | } |
| 72 | } |