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