blob: 0a5584682c2870b24ee92b6d3fd17815732e0499 [file] [log] [blame]
Matthias Andreas Benkard80909242022-02-03 20:47:47 +01001// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
Matthias Andreas Benkard348f2052022-01-15 16:13:01 +01005package eu.mulk.quarkus.googlecloud.jsonlogging.logmanager;
6
7import eu.mulk.quarkus.googlecloud.jsonlogging.Formatter;
8import java.io.InputStream;
9import java.util.Collections;
10import 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 Benkarde369c512022-04-15 20:54:52 +020021 * {@snippet lang="properties" :
Matthias Andreas Benkarddb2440e2022-10-23 12:04:13 +020022 * handlers = eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConsoleHandler
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020023 * }
Matthias Andreas Benkard348f2052022-01-15 16:13:01 +010024 *
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 Benkarde369c512022-04-15 20:54:52 +020039 * {@snippet lang="properties" :
Matthias Andreas Benkard348f2052022-01-15 16:13:01 +010040 * logging.config = classpath:logging.properties
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020041 * }
Matthias Andreas Benkard348f2052022-01-15 16:13:01 +010042 *
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 Benkarde369c512022-04-15 20:54:52 +020047 * {@snippet lang="xml" :
Matthias Andreas Benkard348f2052022-01-15 16:13:01 +010048 * <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 Benkarde369c512022-04-15 20:54:52 +020058 * }
Matthias Andreas Benkard348f2052022-01-15 16:13:01 +010059 *
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")
66public 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}