From 635dd77fa9282b4a3e430ec76e97f15590414e27 Mon Sep 17 00:00:00 2001 From: zml Date: Mon, 25 Apr 2022 19:33:49 -0700 Subject: [PATCH 1/9] text-logger-slf4j: introduce wrapper for formatted logging Fixes GH-731 --- .checkstyle/suppressions.xml | 3 + bom/build.gradle.kts | 1 + gradle/libs.versions.toml | 6 + settings.gradle.kts | 1 + text-logger-slf4j/build.gradle.kts | 18 + .../text/logger/slf4j/CallerClassFinder.java | 43 ++ .../text/logger/slf4j/ComponentLogger.java | 669 ++++++++++++++++++ .../logger/slf4j/ComponentLoggerProvider.java | 76 ++ .../adventure/text/logger/slf4j/Handler.java | 79 +++ .../slf4j/WrappingComponentLoggerImpl.java | 417 +++++++++++ .../text/logger/slf4j/package-info.java | 29 + .../text/logger/slf4j/CallerClassFinder.java | 41 ++ .../logger/slf4j/CallerClassFinderTest.java | 42 ++ .../logger/slf4j/ComponentLoggerTest.java | 39 + 14 files changed, 1464 insertions(+) create mode 100644 text-logger-slf4j/build.gradle.kts create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/package-info.java create mode 100644 text-logger-slf4j/src/main/java9/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java create mode 100644 text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinderTest.java create mode 100644 text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java diff --git a/.checkstyle/suppressions.xml b/.checkstyle/suppressions.xml index 04ed5fffe..ea0de2320 100644 --- a/.checkstyle/suppressions.xml +++ b/.checkstyle/suppressions.xml @@ -8,6 +8,9 @@ + + + diff --git a/bom/build.gradle.kts b/bom/build.gradle.kts index 90e7633c4..51c6a7f99 100644 --- a/bom/build.gradle.kts +++ b/bom/build.gradle.kts @@ -18,6 +18,7 @@ dependencies { "nbt", "serializer-configurate3", "serializer-configurate4", + "text-logger-slf4j", "text-minimessage", "text-serializer-gson", "text-serializer-gson-legacy-impl", diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4032f3d23..36314c4de 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -10,6 +10,7 @@ indra = "2.1.1" jmh = "1.35" jmhPlugin = "0.6.6" junit = "5.8.2" +slf4j = "1.7.36" truth = "1.1.3" [libraries] @@ -28,6 +29,11 @@ kotlin-testJunit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5" } configurate-v3 = "org.spongepowered:configurate-core:3.7.3" configurate-v4 = "org.spongepowered:configurate-core:4.1.2" +# text-logger-slf4j +slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } +slf4j-ext = { module = "org.slf4j:slf4j-ext", version.ref = "slf4j" } +slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" } + # text-serializer-gson gson = "com.google.code.gson:gson:2.8.0" diff --git a/settings.gradle.kts b/settings.gradle.kts index 86282d8f6..24b270f19 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -34,6 +34,7 @@ sequenceOf( "nbt", "serializer-configurate3", "serializer-configurate4", + "text-logger-slf4j", "text-minimessage", "text-serializer-gson", "text-serializer-gson-legacy-impl", diff --git a/text-logger-slf4j/build.gradle.kts b/text-logger-slf4j/build.gradle.kts new file mode 100644 index 000000000..9deaa6341 --- /dev/null +++ b/text-logger-slf4j/build.gradle.kts @@ -0,0 +1,18 @@ +plugins { + id("adventure.common-conventions") +} + +dependencies { + api(projects.adventureApi) + api(libs.slf4j) + implementation(libs.slf4j.ext) { + exclude(group = "org.slf4j", module = "slf4j-api") + } + testImplementation(libs.slf4j.simple) +} + +sourceSets.main { + multirelease { + alternateVersions(9) + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java new file mode 100644 index 000000000..886c89417 --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java @@ -0,0 +1,43 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +// Java 8 version, see Java 9 version as well +final class CallerClassFinder { + private CallerClassFinder() { + } + + static String callingClassName() { + return callingClassName(2); // this, plus the calling method + } + + static String callingClassName(final int elementsToSkip) { // elementsToSkip not counting this method + final StackTraceElement[] elements = Thread.currentThread().getStackTrace(); // includes call to getStackTrace() + if (elements.length <= elementsToSkip) { + throw new IllegalArgumentException("Not enough stack elements to skip " + elementsToSkip + " elements"); + } else { + return elements[elementsToSkip + 2].getClassName(); + } + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java new file mode 100644 index 000000000..66de1cfbb --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java @@ -0,0 +1,669 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.Marker; + +/** + * An extended type of Logger capable of logging formatted components to the console. + * + *

The methods in this logger interface are intended to exactly mirror those methods in {@link Logger} that take a format string, but instead accepting {@link Component}s.

+ * + *

Any {@code arg}s may be passed as Components as well.

+ * + * @since 4.11.0 + */ +public interface ComponentLogger extends Logger { + /** + * Get a logger instance with the name of the calling class. + * + *

This logger is produced by implementations of the {@link ComponentLoggerProvider}.

+ * + * @return a logger with the name of the calling class + * @since 4.11.0 + */ + static ComponentLogger logger() { + return logger(CallerClassFinder.callingClassName()); + } + + /** + * Get a logger instance with the provided name. + * + *

This logger is produced by implementations of the {@link ComponentLoggerProvider}.

+ * + * @param name the name of the logger + * @return a logger with the provided name + * @since 4.11.0 + */ + static ComponentLogger logger(final String name) { + return Handler.logger(name); + } + + /** + * Get a logger instance with the binary name of the provided class. + * + *

This logger is produced by implementations of the {@link ComponentLoggerProvider}.

+ * + * @param clazz the class to use when naming the logger + * @return a logger with the name of the calling class + * @since 4.11.0 + */ + static ComponentLogger logger(final Class clazz) { + return logger(clazz.getName()); + } + + /** + * Log a message at the TRACE level. + * + * @param msg the message string to be logged + * @since 4.11.0 + */ + void trace(final @NotNull Component msg); + + /** + * Log a message at the TRACE level according to the specified format + * and argument. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the TRACE level.

+ * + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void trace(final @NotNull Component format, final @Nullable Object arg); + + /** + * Log a message at the TRACE level according to the specified format + * and arguments. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the TRACE level.

+ * + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void trace(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * Log a message at the TRACE level according to the specified format + * and arguments. + * + *

This form avoids superfluous string concatenation when the logger + * is disabled for the TRACE level. However, this variant incurs the hidden + * (and relatively small) cost of creating an Object[] before invoking the method, + * even if this logger is disabled for TRACE. The variants taking {@link #trace(Component, Object) one} and + * {@link #trace(Component, Object, Object) two} arguments exist solely in order to avoid this hidden cost.

+ * + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void trace(final @NotNull Component format, final @Nullable Object @NotNull... arguments); + + /** + * Log an exception (throwable) at the TRACE level with an + * accompanying message. + * + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void trace(final @NotNull Component msg, final @Nullable Throwable t); + + /** + * Log a message with the specific Marker at the TRACE level. + * + * @param marker the marker data specific to this log statement + * @param msg the message string to be logged + * @since 4.11.0 + */ + void trace(final @NotNull Marker marker, final @NotNull Component msg); + + /** + * This method is similar to {@link #trace(Component, Object)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg); + + /** + * This method is similar to {@link #trace(Component, Object, Object)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * This method is similar to {@link #trace(Component, Object...)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param argArray an array of arguments + * @since 4.11.0 + */ + void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray); + + /** + * This method is similar to {@link #trace(Component, Throwable)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void trace(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t); + + /** + * Log a message at the DEBUG level. + * + * @param msg the message string to be logged + * @since 4.11.0 + */ + void debug(final @NotNull Component msg); + + /** + * Log a message at the DEBUG level according to the specified format + * and argument. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the DEBUG level.

+ * + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void debug(final @NotNull Component format, final @Nullable Object arg); + + /** + * Log a message at the DEBUG level according to the specified format + * and arguments. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the DEBUG level.

+ * + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void debug(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * Log a message at the DEBUG level according to the specified format + * and arguments. + * + *

This form avoids superfluous string concatenation when the logger + * is disabled for the DEBUG level. However, this variant incurs the hidden + * (and relatively small) cost of creating an Object[] before invoking the method, + * even if this logger is disabled for DEBUG. The variants taking + * {@link #debug(Component, Object) one} and {@link #debug(Component, Object, Object) two} + * arguments exist solely in order to avoid this hidden cost.

+ * + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void debug(final @NotNull Component format, final @Nullable Object @NotNull... arguments); + + /** + * Log an exception (throwable) at the DEBUG level with an + * accompanying message. + * + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void debug(final @NotNull Component msg, final @Nullable Throwable t); + + /** + * Log a message with the specific Marker at the DEBUG level. + * + * @param marker the marker data specific to this log statement + * @param msg the message string to be logged + * @since 4.11.0 + */ + void debug(final @NotNull Marker marker, final @NotNull Component msg); + + /** + * This method is similar to {@link #debug(Component, Object)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg); + + /** + * This method is similar to {@link #debug(Component, Object, Object)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * This method is similar to {@link #debug(Component, Object...)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... arguments); + + /** + * This method is similar to {@link #debug(Component, Throwable)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void debug(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t); + + /** + * Log a message at the INFO level. + * + * @param msg the message string to be logged + * @since 4.11.0 + */ + void info(final @NotNull Component msg); + + /** + * Log a message at the INFO level according to the specified format + * and argument. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the INFO level.

+ * + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void info(final @NotNull Component format, final @Nullable Object arg); + + /** + * Log a message at the INFO level according to the specified format + * and arguments. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the INFO level.

+ * + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void info(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * Log a message at the INFO level according to the specified format + * and arguments. + * + *

This form avoids superfluous string concatenation when the logger + * is disabled for the INFO level. However, this variant incurs the hidden + * (and relatively small) cost of creating an Object[] before invoking the method, + * even if this logger is disabled for INFO. The variants taking + * {@link #info(Component, Object) one} and {@link #info(Component, Object, Object) two} + * arguments exist solely in order to avoid this hidden cost.

+ * + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void info(final @NotNull Component format, final @Nullable Object@NotNull... arguments); + + /** + * Log an exception (throwable) at the INFO level with an + * accompanying message. + * + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void info(final @NotNull Component msg, final @Nullable Throwable t); + + /** + * Log a message with the specific Marker at the INFO level. + * + * @param marker The marker specific to this log statement + * @param msg the message string to be logged + * @since 4.11.0 + */ + void info(final @NotNull Marker marker, final @NotNull Component msg); + + /** + * This method is similar to {@link #info(Component, Object)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg); + + /** + * This method is similar to {@link #info(Component, Object, Object)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * This method is similar to {@link #info(Component, Object...)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object@NotNull... arguments); + + /** + * This method is similar to {@link #info(Component, Throwable)} method + * except that the marker data is also taken into consideration. + * + * @param marker the marker data for this log statement + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void info(final @NotNull Marker marker, final @NotNull Component msg, final @NotNull Throwable t); + + /** + * Log a message at the WARN level. + * + * @param msg the message string to be logged + * @since 4.11.0 + */ + void warn(final @NotNull Component msg); + + /** + * Log a message at the WARN level according to the specified format + * and argument. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the WARN level.

+ * + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void warn(final @NotNull Component format, final @Nullable Object arg); + + /** + * Log a message at the WARN level according to the specified format + * and arguments. + * + *

This form avoids superfluous string concatenation when the logger + * is disabled for the WARN level. However, this variant incurs the hidden + * (and relatively small) cost of creating an Object[] before invoking the method, + * even if this logger is disabled for WARN. The variants taking + * {@link #warn(Component, Object) one} and {@link #warn(Component, Object, Object) two} + * arguments exist solely in order to avoid this hidden cost.

+ * + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void warn(final @NotNull Component format, final @Nullable Object@NotNull... arguments); + + /** + * Log a message at the WARN level according to the specified format + * and arguments. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the WARN level.

+ * + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void warn(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * Log an exception (throwable) at the WARN level with an + * accompanying message. + * + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void warn(final @NotNull Component msg, final @NotNull Throwable t); + + /** + * Log a message with the specific final @NotNull Marker at the WARN level. + * + * @param marker The marker specific to this log statement + * @param msg the message string to be logged + * @since 4.11.0 + */ + void warn(final @NotNull Marker marker, final @NotNull Component msg); + + /** + * This method is similar to {@link #warn(Component, Object)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg); + + /** + * This method is similar to {@link #warn(Component, Object, Object)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * This method is similar to {@link #warn(Component, Object...)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object@NotNull... arguments); + + /** + * This method is similar to {@link #warn(Component, Throwable)} method + * except that the marker data is also taken into consideration. + * + * @param marker the marker data for this log statement + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void warn(final @NotNull Marker marker, final @NotNull Component msg, final @NotNull Throwable t); + + /** + * Log a message at the ERROR level. + * + * @param msg the message string to be logged + * @since 4.11.0 + */ + void error(final @NotNull Component msg); + + /** + * Log a message at the ERROR level according to the specified format + * and argument. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the ERROR level.

+ * + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void error(final @NotNull Component format, final @Nullable Object arg); + + /** + * Log a message at the ERROR level according to the specified format + * and arguments. + * + *

This form avoids superfluous object creation when the logger + * is disabled for the ERROR level.

+ * + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void error(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * Log a message at the ERROR level according to the specified format + * and arguments. + * + *

This form avoids superfluous string concatenation when the logger + * is disabled for the ERROR level. However, this variant incurs the hidden + * (and relatively small) cost of creating an Object[] before invoking the method, + * even if this logger is disabled for ERROR. The variants taking + * {@link #error(Component, Object) one} and {@link #error(Component, Object, Object) two} + * arguments exist solely in order to avoid this hidden cost.

+ * + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void error(final @NotNull Component format, final @Nullable Object@NotNull... arguments); + + /** + * Log an exception (throwable) at the ERROR level with an + * accompanying message. + * + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void error(final @NotNull Component msg, final @NotNull Throwable t); + + /** + * Log a message with the specific final @NotNull Marker at the ERROR level. + * + * @param marker The marker specific to this log statement + * @param msg the message string to be logged + * @since 4.11.0 + */ + void error(final @NotNull Marker marker, final @NotNull Component msg); + + /** + * This method is similar to {@link #error(Component, Object)} method except that the + * marker data is also taken into consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg the argument + * @since 4.11.0 + */ + void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg); + + /** + * This method is similar to {@link #error(Component, Object, Object)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arg1 the first argument + * @param arg2 the second argument + * @since 4.11.0 + */ + void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2); + + /** + * This method is similar to {@link #error(Component, Object...)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param format the format string + * @param arguments a list of 3 or more arguments + * @since 4.11.0 + */ + void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object@NotNull... arguments); + + /** + * This method is similar to {@link #error(Component, Throwable)} + * method except that the marker data is also taken into + * consideration. + * + * @param marker the marker data specific to this log statement + * @param msg the message accompanying the exception + * @param t the exception (throwable) to log + * @since 4.11.0 + */ + void error(final @NotNull Marker marker, final @NotNull Component msg, final @NotNull Throwable t); +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java new file mode 100644 index 000000000..ca0e70402 --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java @@ -0,0 +1,76 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import java.util.function.Function; +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; + +/** + * A service interface for platforms to provide their own component logger implementations. + * + * @since 4.11.0 + */ +public interface ComponentLoggerProvider { + /** + * Create a component logger for the provided logger name. + * + * @param helper a source for common helper implementations when building a logger + * @param name the logger name + * @return a component logger with the provided name + * @since 4.11.0 + */ + @NotNull ComponentLogger logger(final @NotNull LoggerHelper helper, final @NotNull String name); + + /** + * A factory for default implementations of component loggers. + * + * @since 4.11.0 + */ + @ApiStatus.NonExtendable + interface LoggerHelper { + + /** + * Create a serializer function that will translate logged output into the system default locale, and then serialize it to plain text. + * + * @return a plain serializer + * @since 4.11.0 + */ + Function plainSerializer(); + + /** + * Create a component logger based on one which delegates to an underlying plain {@link Logger} implementation. + * + *

This sort of logger requires Components to be serialized to some sort of formatted {@link String} to match the SLF4J contract.

+ * + * @param base the base logger + * @param serializer the serializer to translate and format a component in a log message. + * @return a new logger + * @since 4.11.0 + */ + @NotNull ComponentLogger delegating(final @NotNull Logger base, final @NotNull Function serializer); + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java new file mode 100644 index 000000000..068dbbda1 --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java @@ -0,0 +1,79 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import java.util.Locale; +import java.util.function.Function; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.flattener.ComponentFlattener; +import net.kyori.adventure.translation.GlobalTranslator; +import net.kyori.adventure.util.Services; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility methods relating to creating component loggers. + */ +final class Handler { + private static final ComponentLoggerProvider PROVIDER = Services.service(ComponentLoggerProvider.class) + .orElse(LoggerFactory.getILoggerFactory() instanceof ComponentLoggerProvider ? (ComponentLoggerProvider) LoggerFactory.getILoggerFactory() : new DefaultProvider()); + + private Handler() { + } + + static ComponentLogger logger(final String name) { + return PROVIDER.logger(LoggerHelperImpl.INSTANCE, name); + } + + static final class DefaultProvider implements ComponentLoggerProvider { + @Override + public @NotNull ComponentLogger logger(final @NotNull LoggerHelper helper, final @NotNull String name) { + final Logger backing = LoggerFactory.getLogger(name); + return helper.delegating(backing, helper.plainSerializer()); + } + } + + static final class LoggerHelperImpl implements ComponentLoggerProvider.LoggerHelper { + static final LoggerHelperImpl INSTANCE = new LoggerHelperImpl(); + + private LoggerHelperImpl() { + } + + @Override + public Function plainSerializer() { + return comp -> { + final Component translated = GlobalTranslator.render(comp, Locale.getDefault()); + final StringBuilder contents = new StringBuilder(); + ComponentFlattener.basic().flatten(translated, contents::append); + return contents.toString(); + }; + } + + @Override + public @NotNull ComponentLogger delegating(@NotNull final Logger base, @NotNull final Function serializer) { + return new WrappingComponentLoggerImpl(base, serializer); + } + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java new file mode 100644 index 000000000..e0c3a9ebd --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java @@ -0,0 +1,417 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import java.util.function.Function; +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.Marker; +import org.slf4j.ext.LoggerWrapper; + +final class WrappingComponentLoggerImpl extends LoggerWrapper implements ComponentLogger { + private static final String FQCN = WrappingComponentLoggerImpl.class.getName(); + + // TODO: maybe handle ComponentMessageThrowable logging somehow? + + private final Function serializer; + + WrappingComponentLoggerImpl(final Logger backing, final Function serializer) { + super(backing, FQCN); + this.serializer = serializer; + } + + private String serialize(final Component input) { + return this.serializer.apply(input); + } + + private Object maybeSerialize(final @Nullable Object input) { + if (input instanceof Component) { + return this.serialize((Component) input); + } else { + return input; + } + } + + private Object[] maybeSerialize(final @Nullable Object@NotNull... args) { + for (int i = 0; i < args.length; i++) { + if (args[i] instanceof Component) { + args[i] = this.serialize((Component) args[i]); + } + } + + return args; + } + + @Override + public void trace(final @NotNull Component msg) { + if (!this.isTraceEnabled()) return; + + this.trace(this.serialize(msg)); + } + + @Override + public void trace(final @NotNull Component format, final @Nullable Object arg) { + if (!this.isTraceEnabled()) return; + + this.trace(this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void trace(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isTraceEnabled()) return; + + this.trace(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void trace(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { + if (!this.isTraceEnabled()) return; + + this.trace(this.serialize(format), this.maybeSerialize(arguments)); + } + + @Override + public void trace(final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isTraceEnabled()) return; + + this.trace(this.serialize(msg), t); + } + + @Override + public void trace(final Marker marker, final @NotNull Component msg) { + if (!this.isTraceEnabled(marker)) return; + + this.trace(marker, this.serialize(msg)); + } + + @Override + public void trace(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + if (!this.isTraceEnabled(marker)) return; + + this.trace(marker, this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void trace(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isTraceEnabled(marker)) return; + + this.trace(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void trace(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + if (!this.isTraceEnabled(marker)) return; + + this.trace(marker, this.serialize(format), this.maybeSerialize(argArray)); + } + + @Override + public void trace(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isTraceEnabled(marker)) return; + + this.trace(marker, this.serialize(msg), t); + } + + @Override + public void debug(final @NotNull Component msg) { + if (!this.isDebugEnabled()) return; + + this.debug(this.serialize(msg)); + } + + @Override + public void debug(final @NotNull Component format, final @Nullable Object arg) { + if (!this.isDebugEnabled()) return; + + this.debug(this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void debug(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isDebugEnabled()) return; + + this.debug(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void debug(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { + if (!this.isDebugEnabled()) return; + + this.debug(this.serialize(format), this.maybeSerialize(arguments)); + } + + @Override + public void debug(final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isDebugEnabled()) return; + + this.debug(this.serialize(msg), t); + } + + @Override + public void debug(final Marker marker, final @NotNull Component msg) { + if (!this.isDebugEnabled(marker)) return; + + this.debug(marker, this.serialize(msg)); + } + + @Override + public void debug(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + if (!this.isDebugEnabled(marker)) return; + + this.debug(marker, this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void debug(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isDebugEnabled(marker)) return; + + this.debug(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void debug(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + if (!this.isDebugEnabled(marker)) return; + + this.debug(marker, this.serialize(format), this.maybeSerialize(argArray)); + } + + @Override + public void debug(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isDebugEnabled(marker)) return; + + this.debug(marker, this.serialize(msg), t); + } + + @Override + public void info(final @NotNull Component msg) { + if (!this.isInfoEnabled()) return; + + this.info(this.serialize(msg)); + } + + @Override + public void info(final @NotNull Component format, final @Nullable Object arg) { + if (!this.isInfoEnabled()) return; + + this.info(this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void info(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isInfoEnabled()) return; + + this.info(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void info(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { + if (!this.isInfoEnabled()) return; + + this.info(this.serialize(format), this.maybeSerialize(arguments)); + } + + @Override + public void info(final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isInfoEnabled()) return; + + this.info(this.serialize(msg), t); + } + + @Override + public void info(final Marker marker, final @NotNull Component msg) { + if (!this.isInfoEnabled(marker)) return; + + this.info(marker, this.serialize(msg)); + } + + @Override + public void info(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + if (!this.isInfoEnabled(marker)) return; + + this.info(marker, this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void info(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isInfoEnabled(marker)) return; + + this.info(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void info(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + if (!this.isInfoEnabled(marker)) return; + + this.info(marker, this.serialize(format), this.maybeSerialize(argArray)); + } + + @Override + public void info(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isInfoEnabled(marker)) return; + + this.info(marker, this.serialize(msg), t); + } + + @Override + public void warn(final @NotNull Component msg) { + if (!this.isWarnEnabled()) return; + + this.warn(this.serialize(msg)); + } + + @Override + public void warn(final @NotNull Component format, final @Nullable Object arg) { + if (!this.isWarnEnabled()) return; + + this.warn(this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void warn(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isWarnEnabled()) return; + + this.warn(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void warn(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { + if (!this.isWarnEnabled()) return; + + this.warn(this.serialize(format), this.maybeSerialize(arguments)); + } + + @Override + public void warn(final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isWarnEnabled()) return; + + this.warn(this.serialize(msg), t); + } + + @Override + public void warn(final Marker marker, final @NotNull Component msg) { + if (!this.isWarnEnabled(marker)) return; + + this.warn(marker, this.serialize(msg)); + } + + @Override + public void warn(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + if (!this.isWarnEnabled(marker)) return; + + this.warn(marker, this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void warn(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isWarnEnabled(marker)) return; + + this.warn(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void warn(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + if (!this.isWarnEnabled(marker)) return; + + this.warn(marker, this.serialize(format), this.maybeSerialize(argArray)); + } + + @Override + public void warn(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isWarnEnabled(marker)) return; + + this.warn(marker, this.serialize(msg), t); + } + + @Override + public void error(final @NotNull Component msg) { + if (!this.isErrorEnabled()) return; + + this.error(this.serialize(msg)); + } + + @Override + public void error(final @NotNull Component format, final @Nullable Object arg) { + if (!this.isErrorEnabled()) return; + + this.error(this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void error(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isErrorEnabled()) return; + + this.error(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void error(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { + if (!this.isErrorEnabled()) return; + + this.error(this.serialize(format), this.maybeSerialize(arguments)); + } + + @Override + public void error(final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isErrorEnabled()) return; + + this.error(this.serialize(msg), t); + } + + @Override + public void error(final Marker marker, final @NotNull Component msg) { + if (!this.isErrorEnabled(marker)) return; + + this.error(marker, this.serialize(msg)); + } + + @Override + public void error(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + if (!this.isErrorEnabled(marker)) return; + + this.error(marker, this.serialize(format), this.maybeSerialize(arg)); + } + + @Override + public void error(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isErrorEnabled(marker)) return; + + this.error(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + + @Override + public void error(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + if (!this.isErrorEnabled(marker)) return; + + this.error(marker, this.serialize(format), this.maybeSerialize(argArray)); + } + + @Override + public void error(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + if (!this.isErrorEnabled(marker)) return; + + this.error(marker, this.serialize(msg), t); + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/package-info.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/package-info.java new file mode 100644 index 000000000..04aa92e82 --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/package-info.java @@ -0,0 +1,29 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +/** + * A wrapper around SLF4J providing methods for formatted logging of Components. + * + *

This wrapper supports the API provided in 1.7/1.8, but does not yet implement the fluent API present in the 2.0 betas.

+ */ +package net.kyori.adventure.text.logger.slf4j; diff --git a/text-logger-slf4j/src/main/java9/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java b/text-logger-slf4j/src/main/java9/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java new file mode 100644 index 000000000..0054a096a --- /dev/null +++ b/text-logger-slf4j/src/main/java9/net/kyori/adventure/text/logger/slf4j/CallerClassFinder.java @@ -0,0 +1,41 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +// Java 9+ version, see Java 8 version as well +final class CallerClassFinder { + private CallerClassFinder() { + } + + static String callingClassName() { + return callingClassName(2); // this, plus the calling method + } + + static String callingClassName(final int elementsToSkip) { // elementsToSkip not counting this method + return StackWalker.getInstance().walk(stream -> stream.map(StackWalker.StackFrame::getClassName) + .skip(elementsToSkip + 1) + .findFirst()) + .orElseThrow(() -> new IllegalArgumentException("Not enough stack elements to skip " + elementsToSkip + " elements")); + } +} diff --git a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinderTest.java b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinderTest.java new file mode 100644 index 000000000..afe3240ea --- /dev/null +++ b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/CallerClassFinderTest.java @@ -0,0 +1,42 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CallerClassFinderTest { + + @Test + void testCallerClass() { + assertEquals(CallerClassFinderTest.class.getName(), Holder.test()); + } + + static final class Holder { + static String test() { + return CallerClassFinder.callingClassName(); + } + } +} diff --git a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java new file mode 100644 index 000000000..c9103c6c4 --- /dev/null +++ b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java @@ -0,0 +1,39 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.junit.jupiter.api.Test; + +public class ComponentLoggerTest { + @Test + void testLogSimple() { + final Component toLog = Component.text().content("Hello ").color(NamedTextColor.RED).append(Component.translatable("location.world")).build(); + + final ComponentLogger logger = ComponentLogger.logger(); + + logger.info(toLog); + } +} From 7a240bb9ead5963e30d0467e17fdc105a986c154 Mon Sep 17 00:00:00 2001 From: zml Date: Mon, 25 Apr 2022 22:10:40 -0700 Subject: [PATCH 2/9] text-logger-slf4j: some cleanup --- text-logger-slf4j/build.gradle.kts | 15 ++++++++++++--- .../text/logger/slf4j/ComponentLogger.java | 12 ++++++++---- .../logger/slf4j/ComponentLoggerProvider.java | 2 +- .../adventure/text/logger/slf4j/Handler.java | 10 +++++++++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/text-logger-slf4j/build.gradle.kts b/text-logger-slf4j/build.gradle.kts index 9deaa6341..b438f419c 100644 --- a/text-logger-slf4j/build.gradle.kts +++ b/text-logger-slf4j/build.gradle.kts @@ -5,9 +5,7 @@ plugins { dependencies { api(projects.adventureApi) api(libs.slf4j) - implementation(libs.slf4j.ext) { - exclude(group = "org.slf4j", module = "slf4j-api") - } + implementation(libs.slf4j.ext) testImplementation(libs.slf4j.simple) } @@ -16,3 +14,14 @@ sourceSets.main { alternateVersions(9) } } + +eclipse { + // Make sure slf4j doesn't end up on the module path until we are actually a module + classpath.file.whenMerged { + (this as org.gradle.plugins.ide.eclipse.model.Classpath).entries.forEach { entry -> + if (entry is org.gradle.plugins.ide.eclipse.model.Library) { + entry.entryAttributes["module"] = false + } + } + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java index 66de1cfbb..462dbb454 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java @@ -29,6 +29,8 @@ import org.slf4j.Logger; import org.slf4j.Marker; +import static java.util.Objects.requireNonNull; + /** * An extended type of Logger capable of logging formatted components to the console. * @@ -42,12 +44,14 @@ public interface ComponentLogger extends Logger { /** * Get a logger instance with the name of the calling class. * + *

This method is caller-sensitive and should not be wrapped. See + * *

This logger is produced by implementations of the {@link ComponentLoggerProvider}.

* * @return a logger with the name of the calling class * @since 4.11.0 */ - static ComponentLogger logger() { + static @NotNull ComponentLogger logger() { return logger(CallerClassFinder.callingClassName()); } @@ -60,8 +64,8 @@ static ComponentLogger logger() { * @return a logger with the provided name * @since 4.11.0 */ - static ComponentLogger logger(final String name) { - return Handler.logger(name); + static @NotNull ComponentLogger logger(final @NotNull String name) { + return Handler.logger(requireNonNull(name, "name")); } /** @@ -73,7 +77,7 @@ static ComponentLogger logger(final String name) { * @return a logger with the name of the calling class * @since 4.11.0 */ - static ComponentLogger logger(final Class clazz) { + static @NotNull ComponentLogger logger(final @NotNull Class clazz) { return logger(clazz.getName()); } diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java index ca0e70402..ba97cfd36 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java @@ -59,7 +59,7 @@ interface LoggerHelper { * @return a plain serializer * @since 4.11.0 */ - Function plainSerializer(); + @NotNull Function plainSerializer(); /** * Create a component logger based on one which delegates to an underlying plain {@link Logger} implementation. diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java index 068dbbda1..8c556bd1a 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java @@ -24,6 +24,8 @@ package net.kyori.adventure.text.logger.slf4j; import java.util.Locale; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.flattener.ComponentFlattener; @@ -48,10 +50,16 @@ static ComponentLogger logger(final String name) { } static final class DefaultProvider implements ComponentLoggerProvider { + private final Map loggers = new ConcurrentHashMap<>(); @Override public @NotNull ComponentLogger logger(final @NotNull LoggerHelper helper, final @NotNull String name) { + final ComponentLogger initial = this.loggers.get(name); + if (initial != null) return initial; + final Logger backing = LoggerFactory.getLogger(name); - return helper.delegating(backing, helper.plainSerializer()); + final ComponentLogger created = helper.delegating(backing, helper.plainSerializer()); + final ComponentLogger existing = this.loggers.putIfAbsent(name, created); + return existing == null ? created : existing; } } From 00516706ff4f1113b8cbb7deb8250ef7290ffb7d Mon Sep 17 00:00:00 2001 From: zml Date: Thu, 19 May 2022 19:38:01 -0700 Subject: [PATCH 3/9] fix codestyle --- .../net/kyori/adventure/text/logger/slf4j/ComponentLogger.java | 2 +- .../java/net/kyori/adventure/text/logger/slf4j/Handler.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java index 462dbb454..63a027938 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLogger.java @@ -44,7 +44,7 @@ public interface ComponentLogger extends Logger { /** * Get a logger instance with the name of the calling class. * - *

This method is caller-sensitive and should not be wrapped. See + *

This method is caller-sensitive and should not be wrapped.

* *

This logger is produced by implementations of the {@link ComponentLoggerProvider}.

* diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java index 8c556bd1a..2c0b90639 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java @@ -51,6 +51,7 @@ static ComponentLogger logger(final String name) { static final class DefaultProvider implements ComponentLoggerProvider { private final Map loggers = new ConcurrentHashMap<>(); + @Override public @NotNull ComponentLogger logger(final @NotNull LoggerHelper helper, final @NotNull String name) { final ComponentLogger initial = this.loggers.get(name); @@ -80,7 +81,7 @@ public Function plainSerializer() { } @Override - public @NotNull ComponentLogger delegating(@NotNull final Logger base, @NotNull final Function serializer) { + public @NotNull ComponentLogger delegating(final @NotNull Logger base, final @NotNull Function serializer) { return new WrappingComponentLoggerImpl(base, serializer); } } From ba4e9ef7a3fbe073129752011a77fdec7780daf2 Mon Sep 17 00:00:00 2001 From: zml Date: Thu, 19 May 2022 21:48:37 -0700 Subject: [PATCH 4/9] text-logger-slf4j: We have a test now --- gradle/libs.versions.toml | 4 +++ text-logger-slf4j/build.gradle.kts | 3 +++ .../adventure/text/logger/slf4j/Handler.java | 2 +- .../logger/slf4j/ComponentLoggerTest.java | 27 ++++++++++++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 36314c4de..d186152fc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -10,6 +10,7 @@ indra = "2.1.1" jmh = "1.35" jmhPlugin = "0.6.6" junit = "5.8.2" +mockito = "4.5.1" slf4j = "1.7.36" truth = "1.1.3" @@ -33,6 +34,9 @@ configurate-v4 = "org.spongepowered:configurate-core:4.1.2" slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } slf4j-ext = { module = "org.slf4j:slf4j-ext", version.ref = "slf4j" } slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" } +mockSlf4j = "org.simplify4u:slf4j-mock:2.2.0" +mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" } +mockito-junit = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" } # text-serializer-gson gson = "com.google.code.gson:gson:2.8.0" diff --git a/text-logger-slf4j/build.gradle.kts b/text-logger-slf4j/build.gradle.kts index b438f419c..b99c21c54 100644 --- a/text-logger-slf4j/build.gradle.kts +++ b/text-logger-slf4j/build.gradle.kts @@ -7,6 +7,9 @@ dependencies { api(libs.slf4j) implementation(libs.slf4j.ext) testImplementation(libs.slf4j.simple) + testImplementation(libs.mockSlf4j) + testImplementation(libs.mockito) + testImplementation(libs.mockito.junit) } sourceSets.main { diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java index 2c0b90639..33d8c85cd 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/Handler.java @@ -67,7 +67,7 @@ static final class DefaultProvider implements ComponentLoggerProvider { static final class LoggerHelperImpl implements ComponentLoggerProvider.LoggerHelper { static final LoggerHelperImpl INSTANCE = new LoggerHelperImpl(); - private LoggerHelperImpl() { + LoggerHelperImpl() { } @Override diff --git a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java index c9103c6c4..53d9ef331 100644 --- a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java +++ b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java @@ -25,15 +25,40 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; +@ExtendWith(MockitoExtension.class) public class ComponentLoggerTest { + @Mock(name = "net.kyori.adventure.test.ComponentLoggerTest") + Logger backingLogger; + + private final ComponentLoggerProvider.LoggerHelper helper = new Handler.LoggerHelperImpl(); + + ComponentLogger makeLogger() { + return new WrappingComponentLoggerImpl(this.backingLogger, this.helper.plainSerializer()); + } + + @BeforeEach + void enableLevels() { + Mockito.when(this.backingLogger.isInfoEnabled()).thenReturn(true); + } + @Test void testLogSimple() { final Component toLog = Component.text().content("Hello ").color(NamedTextColor.RED).append(Component.translatable("location.world")).build(); - final ComponentLogger logger = ComponentLogger.logger(); + final ComponentLogger logger = this.makeLogger(); logger.info(toLog); + + Mockito.verify(this.backingLogger, Mockito.times(2)).isInfoEnabled(); + Mockito.verify(this.backingLogger).info("Hello location.world"); + Mockito.verifyNoMoreInteractions(this.backingLogger); } } From 56b5d4fad46b58c4c9f02a49ef5e6bc68b3019b9 Mon Sep 17 00:00:00 2001 From: zml Date: Sat, 21 May 2022 17:20:25 -0700 Subject: [PATCH 5/9] Improve test coverage, unpack component msgs on throwables --- gradle/libs.versions.toml | 5 +- text-logger-slf4j/build.gradle.kts | 5 +- .../slf4j/UnpackedComponentThrowable.java | 73 +++++++++++ .../slf4j/WrappingComponentLoggerImpl.java | 35 ++--- .../logger/slf4j/ComponentLoggerTest.java | 124 +++++++++++++++--- 5 files changed, 199 insertions(+), 43 deletions(-) create mode 100644 text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/UnpackedComponentThrowable.java diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d186152fc..77f47173a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -33,10 +33,7 @@ configurate-v4 = "org.spongepowered:configurate-core:4.1.2" # text-logger-slf4j slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } slf4j-ext = { module = "org.slf4j:slf4j-ext", version.ref = "slf4j" } -slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" } -mockSlf4j = "org.simplify4u:slf4j-mock:2.2.0" -mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" } -mockito-junit = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" } +slf4jtest = "com.github.valfirst:slf4j-test:2.6.1" # Specific versions are needed for different SLF4J versions # text-serializer-gson gson = "com.google.code.gson:gson:2.8.0" diff --git a/text-logger-slf4j/build.gradle.kts b/text-logger-slf4j/build.gradle.kts index b99c21c54..c2705f3a3 100644 --- a/text-logger-slf4j/build.gradle.kts +++ b/text-logger-slf4j/build.gradle.kts @@ -6,10 +6,7 @@ dependencies { api(projects.adventureApi) api(libs.slf4j) implementation(libs.slf4j.ext) - testImplementation(libs.slf4j.simple) - testImplementation(libs.mockSlf4j) - testImplementation(libs.mockito) - testImplementation(libs.mockito.junit) + testImplementation(libs.slf4jtest) } sourceSets.main { diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/UnpackedComponentThrowable.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/UnpackedComponentThrowable.java new file mode 100644 index 000000000..25388b1f4 --- /dev/null +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/UnpackedComponentThrowable.java @@ -0,0 +1,73 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.logger.slf4j; + +import java.util.function.Function; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.util.ComponentMessageThrowable; +import org.jetbrains.annotations.Nullable; + +/** + * A wrapper for exceptions that implement ComponentMessageThrowable. + */ +final class UnpackedComponentThrowable extends Throwable { + private static final long serialVersionUID = -1L; + + private final Class backingType; + + static Throwable unpack(final Throwable maybeRich, final Function serializer) { + if (!(maybeRich instanceof ComponentMessageThrowable)) return maybeRich; // TODO: do we need to unwrap any nested exceptions? + + final @Nullable Component message = ((ComponentMessageThrowable) maybeRich).componentMessage(); + final Throwable cause = maybeRich.getCause() != null ? unpack(maybeRich.getCause(), serializer) : null; + final Throwable[] suppressed = maybeRich.getSuppressed(); + + final UnpackedComponentThrowable ret = new UnpackedComponentThrowable(maybeRich.getClass(), serializer.apply(message), cause); + ret.setStackTrace(maybeRich.getStackTrace()); + if (suppressed.length > 0) { + for (int i = 0; i < suppressed.length; i++) { + ret.addSuppressed(unpack(suppressed[i], serializer)); + } + } + + return ret; + } + + private UnpackedComponentThrowable(final Class backingType, final String serializedMessage, final Throwable cause) { + super(serializedMessage, cause); + this.backingType = backingType; + } + + @Override + public String toString() { + final String className = this.backingType.getName(); + final String message = this.getMessage(); + return message == null ? className : className + ":" + message; + } + + @Override + public synchronized Throwable fillInStackTrace() { + return this; + } +} diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java index e0c3a9ebd..4db572080 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java @@ -25,6 +25,7 @@ import java.util.function.Function; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -34,8 +35,6 @@ final class WrappingComponentLoggerImpl extends LoggerWrapper implements ComponentLogger { private static final String FQCN = WrappingComponentLoggerImpl.class.getName(); - // TODO: maybe handle ComponentMessageThrowable logging somehow? - private final Function serializer; WrappingComponentLoggerImpl(final Logger backing, final Function serializer) { @@ -48,8 +47,8 @@ private String serialize(final Component input) { } private Object maybeSerialize(final @Nullable Object input) { - if (input instanceof Component) { - return this.serialize((Component) input); + if (input instanceof ComponentLike) { + return this.serialize(((ComponentLike) input).asComponent()); } else { return input; } @@ -57,11 +56,15 @@ private Object maybeSerialize(final @Nullable Object input) { private Object[] maybeSerialize(final @Nullable Object@NotNull... args) { for (int i = 0; i < args.length; i++) { - if (args[i] instanceof Component) { - args[i] = this.serialize((Component) args[i]); + if (args[i] instanceof ComponentLike) { + args[i] = this.serialize(((ComponentLike) args[i]).asComponent()); } } + if (args.length > 0 && args[args.length - 1] instanceof Throwable) { + args[args.length - 1] = UnpackedComponentThrowable.unpack((Throwable) args[args.length - 1], this.serializer); + } + return args; } @@ -97,7 +100,7 @@ public void trace(final @NotNull Component format, final @Nullable Object @NotNu public void trace(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isTraceEnabled()) return; - this.trace(this.serialize(msg), t); + this.trace(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -132,7 +135,7 @@ public void trace(final Marker marker, final @NotNull Component format, final @N public void trace(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isTraceEnabled(marker)) return; - this.trace(marker, this.serialize(msg), t); + this.trace(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -167,7 +170,7 @@ public void debug(final @NotNull Component format, final @Nullable Object @NotNu public void debug(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isDebugEnabled()) return; - this.debug(this.serialize(msg), t); + this.debug(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -202,7 +205,7 @@ public void debug(final Marker marker, final @NotNull Component format, final @N public void debug(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isDebugEnabled(marker)) return; - this.debug(marker, this.serialize(msg), t); + this.debug(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -237,7 +240,7 @@ public void info(final @NotNull Component format, final @Nullable Object @NotNul public void info(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isInfoEnabled()) return; - this.info(this.serialize(msg), t); + this.info(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -272,7 +275,7 @@ public void info(final Marker marker, final @NotNull Component format, final @Nu public void info(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isInfoEnabled(marker)) return; - this.info(marker, this.serialize(msg), t); + this.info(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -307,7 +310,7 @@ public void warn(final @NotNull Component format, final @Nullable Object @NotNul public void warn(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isWarnEnabled()) return; - this.warn(this.serialize(msg), t); + this.warn(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -342,7 +345,7 @@ public void warn(final Marker marker, final @NotNull Component format, final @Nu public void warn(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isWarnEnabled(marker)) return; - this.warn(marker, this.serialize(msg), t); + this.warn(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -377,7 +380,7 @@ public void error(final @NotNull Component format, final @Nullable Object @NotNu public void error(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isErrorEnabled()) return; - this.error(this.serialize(msg), t); + this.error(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } @Override @@ -412,6 +415,6 @@ public void error(final Marker marker, final @NotNull Component format, final @N public void error(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isErrorEnabled(marker)) return; - this.error(marker, this.serialize(msg), t); + this.error(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } diff --git a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java index 53d9ef331..cffb91fb7 100644 --- a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java +++ b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java @@ -23,42 +23,128 @@ */ package net.kyori.adventure.text.logger.slf4j; +import com.github.valfirst.slf4jtest.LoggingEvent; +import com.github.valfirst.slf4jtest.TestLogger; +import com.github.valfirst.slf4jtest.TestLoggerFactory; +import com.github.valfirst.slf4jtest.TestLoggerFactoryExtension; +import com.google.common.collect.ImmutableList; +import java.util.List; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import org.junit.jupiter.api.BeforeEach; +import net.kyori.adventure.util.ComponentMessageThrowable; +import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import org.slf4j.Logger; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; -@ExtendWith(MockitoExtension.class) +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ExtendWith(TestLoggerFactoryExtension.class) public class ComponentLoggerTest { - @Mock(name = "net.kyori.adventure.test.ComponentLoggerTest") - Logger backingLogger; + private static final TestLogger LOGGER = TestLoggerFactory.getTestLogger(ComponentLoggerTest.class); - private final ComponentLoggerProvider.LoggerHelper helper = new Handler.LoggerHelperImpl(); + private static final Marker MARKED = MarkerFactory.getMarker("MARKED"); ComponentLogger makeLogger() { - return new WrappingComponentLoggerImpl(this.backingLogger, this.helper.plainSerializer()); + return ComponentLogger.logger(); } - @BeforeEach - void enableLevels() { - Mockito.when(this.backingLogger.isInfoEnabled()).thenReturn(true); + @Test + void testCallerLogger() { + final ComponentLogger logger = ComponentLogger.logger(); + assertEquals(this.getClass().getName(), logger.getName()); } @Test void testLogSimple() { - final Component toLog = Component.text().content("Hello ").color(NamedTextColor.RED).append(Component.translatable("location.world")).build(); + final Component toLog = Component.text() + .content("Hello ") + .color(NamedTextColor.RED) + .append(Component.translatable("location.world")) + .build(); + + this.makeLogger().info(toLog); + + assertEquals(LOGGER.getLoggingEvents(), ImmutableList.of(LoggingEvent.info("Hello location.world"))); + } + + @Test + void testComponentArg() { + final Component message = Component.text("Hello ").append(Component.text("{}", NamedTextColor.BLUE)); + final Component arg = Component.selector("@s"); - final ComponentLogger logger = this.makeLogger(); + this.makeLogger().warn(message, arg); - logger.info(toLog); + assertEquals(LOGGER.getLoggingEvents(), ImmutableList.of(LoggingEvent.warn("Hello {}", "@s"))); + } + + @Test + void testStringArg() { + final Component message = Component.text("Hello ").append(Component.text("{}", NamedTextColor.BLUE)); + final String arg = "world"; + + this.makeLogger().debug(message, arg); + + assertEquals(LOGGER.getLoggingEvents(), ImmutableList.of(LoggingEvent.debug("Hello {}", arg))); + } + + @Test + void testMultiArgs() { + final Component message = Component.text("Good morning! The time is {} and you have {} cats!"); + final Component arg0 = Component.text("14:28", NamedTextColor.BLUE); + final String arg1 = "11"; - Mockito.verify(this.backingLogger, Mockito.times(2)).isInfoEnabled(); - Mockito.verify(this.backingLogger).info("Hello location.world"); - Mockito.verifyNoMoreInteractions(this.backingLogger); + this.makeLogger().error(message, arg0, arg1); + assertEquals( + LOGGER.getLoggingEvents(), + ImmutableList.of(LoggingEvent.error("Good morning! The time is {} and you have {} cats!", "14:28", "11")) + ); + } + + @Test + void testUnwrapThrowable() { + final Component message = Component.text("Hello world"); + final Exception error = new RichTestException(Component.translatable("test.failed", NamedTextColor.DARK_PURPLE)); + + this.makeLogger().warn(message, error); + + final List events = LOGGER.getLoggingEvents(); + assertEquals(1, events.size()); + final Throwable thrownException = events.get(0).getThrowable().orElse(null); + assertNotNull(thrownException); + + assertEquals("test.failed", thrownException.getMessage()); + assertArrayEquals(error.getStackTrace(), thrownException.getStackTrace()); + assertTrue(thrownException.toString().startsWith("net.kyori.adventure.text.logger.slf4j.ComponentLoggerTest$RichTestException")); + } + + static class RichTestException extends Exception implements ComponentMessageThrowable { + private static final long serialVersionUID = -1l; + + private final Component richMessage; + + RichTestException(final Component richMessage) { + super("no"); + this.richMessage = richMessage; + } + + @Override + public @Nullable Component componentMessage() { + return this.richMessage; + } + } + + @Test + void testWithMarker() { + final Component message = Component.text("meow :3"); + this.makeLogger().info(MARKED, message); + assertEquals( + LOGGER.getLoggingEvents(), + ImmutableList.of(LoggingEvent.info(MARKED, "meow :3")) + ); } } From 842e2135704366d1d97685a55c847dc7da747937 Mon Sep 17 00:00:00 2001 From: zml Date: Sat, 21 May 2022 19:04:19 -0700 Subject: [PATCH 6/9] text-logger-slf4j: Properly delegate to LocationAwareLoggers --- .../slf4j/WrappingComponentLoggerImpl.java | 715 ++++++++++++++++-- 1 file changed, 634 insertions(+), 81 deletions(-) diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java index 4db572080..05cbbb33f 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java @@ -31,6 +31,7 @@ import org.slf4j.Logger; import org.slf4j.Marker; import org.slf4j.ext.LoggerWrapper; +import org.slf4j.spi.LocationAwareLogger; final class WrappingComponentLoggerImpl extends LoggerWrapper implements ComponentLogger { private static final String FQCN = WrappingComponentLoggerImpl.class.getName(); @@ -38,11 +39,13 @@ final class WrappingComponentLoggerImpl extends LoggerWrapper implements Compone private final Function serializer; WrappingComponentLoggerImpl(final Logger backing, final Function serializer) { - super(backing, FQCN); + super(backing, LoggerWrapper.class.getName()); this.serializer = serializer; } private String serialize(final Component input) { + if (input == null) return null; + return this.serializer.apply(input); } @@ -69,352 +72,902 @@ private Object[] maybeSerialize(final @Nullable Object@NotNull... args) { } @Override - public void trace(final @NotNull Component msg) { + public void trace(final @NotNull Component format) { if (!this.isTraceEnabled()) return; - this.trace(this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + null, + null + ); + } else { + this.trace(this.serialize(format)); + } } @Override public void trace(final @NotNull Component format, final @Nullable Object arg) { if (!this.isTraceEnabled()) return; - this.trace(this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.trace(this.serialize(format), this.maybeSerialize(arg)); + } } @Override public void trace(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isTraceEnabled()) return; - this.trace(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.trace(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override public void trace(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isTraceEnabled()) return; - this.trace(this.serialize(format), this.maybeSerialize(arguments)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + this.maybeSerialize(arguments), + null + ); + } else { + this.trace(this.serialize(format), this.maybeSerialize(arguments)); + } } @Override public void trace(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isTraceEnabled()) return; - this.trace(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.trace(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void trace(final Marker marker, final @NotNull Component msg) { + public void trace(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isTraceEnabled(marker)) return; - this.trace(marker, this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(msg), + null, + null + ); + } else { + this.trace(marker, this.serialize(msg)); + } } @Override - public void trace(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + public void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isTraceEnabled(marker)) return; - this.trace(marker, this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.trace(marker, this.serialize(format), this.maybeSerialize(arg)); + } } @Override - public void trace(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + public void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isTraceEnabled(marker)) return; - this.trace(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.trace(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override - public void trace(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + public void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isTraceEnabled(marker)) return; - this.trace(marker, this.serialize(format), this.maybeSerialize(argArray)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(format), + this.maybeSerialize(argArray), + null + ); + } else { + this.trace(marker, this.serialize(format), this.maybeSerialize(argArray)); + } } @Override - public void trace(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + public void trace(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isTraceEnabled(marker)) return; - this.trace(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.trace(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void debug(final @NotNull Component msg) { + public void debug(final @NotNull Component format) { if (!this.isDebugEnabled()) return; - this.debug(this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + null, + null + ); + } else { + this.debug(this.serialize(format)); + } } @Override public void debug(final @NotNull Component format, final @Nullable Object arg) { if (!this.isDebugEnabled()) return; - this.debug(this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.debug(this.serialize(format), this.maybeSerialize(arg)); + } } @Override public void debug(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isDebugEnabled()) return; - this.debug(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.debug(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override public void debug(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isDebugEnabled()) return; - this.debug(this.serialize(format), this.maybeSerialize(arguments)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + this.maybeSerialize(arguments), + null + ); + } else { + this.debug(this.serialize(format), this.maybeSerialize(arguments)); + } } @Override public void debug(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isDebugEnabled()) return; - this.debug(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.debug(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void debug(final Marker marker, final @NotNull Component msg) { + public void debug(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isDebugEnabled(marker)) return; - this.debug(marker, this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(msg), + null, + null + ); + } else { + this.debug(marker, this.serialize(msg)); + } } @Override - public void debug(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + public void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isDebugEnabled(marker)) return; - this.debug(marker, this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.debug(marker, this.serialize(format), this.maybeSerialize(arg)); + } } @Override - public void debug(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + public void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isDebugEnabled(marker)) return; - this.debug(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.debug(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override - public void debug(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + public void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isDebugEnabled(marker)) return; - this.debug(marker, this.serialize(format), this.maybeSerialize(argArray)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(format), + this.maybeSerialize(argArray), + null + ); + } else { + this.debug(marker, this.serialize(format), this.maybeSerialize(argArray)); + } } @Override - public void debug(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + public void debug(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isDebugEnabled(marker)) return; - this.debug(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.debug(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void info(final @NotNull Component msg) { + public void info(final @NotNull Component format) { if (!this.isInfoEnabled()) return; - this.info(this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + null, + null + ); + } else { + this.info(this.serialize(format)); + } } @Override public void info(final @NotNull Component format, final @Nullable Object arg) { if (!this.isInfoEnabled()) return; - this.info(this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.info(this.serialize(format), this.maybeSerialize(arg)); + } } @Override public void info(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isInfoEnabled()) return; - this.info(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.info(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override public void info(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isInfoEnabled()) return; - this.info(this.serialize(format), this.maybeSerialize(arguments)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + this.maybeSerialize(arguments), + null + ); + } else { + this.info(this.serialize(format), this.maybeSerialize(arguments)); + } } @Override public void info(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isInfoEnabled()) return; - this.info(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.info(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void info(final Marker marker, final @NotNull Component msg) { + public void info(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isInfoEnabled(marker)) return; - this.info(marker, this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(msg), + null, + null + ); + } else { + this.info(marker, this.serialize(msg)); + } } @Override - public void info(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + public void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isInfoEnabled(marker)) return; - this.info(marker, this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.info(marker, this.serialize(format), this.maybeSerialize(arg)); + } } @Override - public void info(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + public void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isInfoEnabled(marker)) return; - this.info(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.info(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override - public void info(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + public void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isInfoEnabled(marker)) return; - this.info(marker, this.serialize(format), this.maybeSerialize(argArray)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(format), + this.maybeSerialize(argArray), + null + ); + } else { + this.info(marker, this.serialize(format), this.maybeSerialize(argArray)); + } } @Override - public void info(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + public void info(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isInfoEnabled(marker)) return; - this.info(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.info(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void warn(final @NotNull Component msg) { + public void warn(final @NotNull Component format) { if (!this.isWarnEnabled()) return; - this.warn(this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + null, + null + ); + } else { + this.warn(this.serialize(format)); + } } @Override public void warn(final @NotNull Component format, final @Nullable Object arg) { if (!this.isWarnEnabled()) return; - this.warn(this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.warn(this.serialize(format), this.maybeSerialize(arg)); + } } @Override public void warn(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isWarnEnabled()) return; - this.warn(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.warn(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override public void warn(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isWarnEnabled()) return; - this.warn(this.serialize(format), this.maybeSerialize(arguments)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + this.maybeSerialize(arguments), + null + ); + } else { + this.warn(this.serialize(format), this.maybeSerialize(arguments)); + } } @Override public void warn(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isWarnEnabled()) return; - this.warn(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.warn(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void warn(final Marker marker, final @NotNull Component msg) { + public void warn(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isWarnEnabled(marker)) return; - this.warn(marker, this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(msg), + null, + null + ); + } else { + this.warn(marker, this.serialize(msg)); + } } @Override - public void warn(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + public void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isWarnEnabled(marker)) return; - this.warn(marker, this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.warn(marker, this.serialize(format), this.maybeSerialize(arg)); + } } @Override - public void warn(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + public void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isWarnEnabled(marker)) return; - this.warn(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.warn(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override - public void warn(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + public void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isWarnEnabled(marker)) return; - this.warn(marker, this.serialize(format), this.maybeSerialize(argArray)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(format), + this.maybeSerialize(argArray), + null + ); + } else { + this.warn(marker, this.serialize(format), this.maybeSerialize(argArray)); + } } @Override - public void warn(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + public void warn(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isWarnEnabled(marker)) return; - this.warn(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.warn(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void error(final @NotNull Component msg) { + public void error(final @NotNull Component format) { if (!this.isErrorEnabled()) return; - this.error(this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + null, + null + ); + } else { + this.error(this.serialize(format)); + } } @Override public void error(final @NotNull Component format, final @Nullable Object arg) { if (!this.isErrorEnabled()) return; - this.error(this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.error(this.serialize(format), this.maybeSerialize(arg)); + } } @Override public void error(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isErrorEnabled()) return; - this.error(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.error(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override public void error(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isErrorEnabled()) return; - this.error(this.serialize(format), this.maybeSerialize(arguments)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + this.maybeSerialize(arguments), + null + ); + } else { + this.error(this.serialize(format), this.maybeSerialize(arguments)); + } } @Override public void error(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isErrorEnabled()) return; - this.error(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.error(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } @Override - public void error(final Marker marker, final @NotNull Component msg) { + public void error(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isErrorEnabled(marker)) return; - this.error(marker, this.serialize(msg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(msg), + null, + null + ); + } else { + this.error(marker, this.serialize(msg)); + } } @Override - public void error(final Marker marker, final @NotNull Component format, final @Nullable Object arg) { + public void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isErrorEnabled(marker)) return; - this.error(marker, this.serialize(format), this.maybeSerialize(arg)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.error(marker, this.serialize(format), this.maybeSerialize(arg)); + } } @Override - public void error(final Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { + public void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isErrorEnabled(marker)) return; - this.error(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.error(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } } @Override - public void error(final Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { + public void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isErrorEnabled(marker)) return; - this.error(marker, this.serialize(format), this.maybeSerialize(argArray)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(format), + this.maybeSerialize(argArray), + null + ); + } else { + this.error(marker, this.serialize(format), this.maybeSerialize(argArray)); + } } @Override - public void error(final Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { + public void error(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isErrorEnabled(marker)) return; - this.error(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + if (this.instanceofLAL) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + this.serialize(msg), + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.error(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + } } } From 50857fa9fd27223eb47995fdf26f25274dcc545b Mon Sep 17 00:00:00 2001 From: zml Date: Sat, 21 May 2022 20:22:53 -0700 Subject: [PATCH 7/9] text-logger-slf4j: Support Components being passed as arguments to regular logging methods --- gradle/libs.versions.toml | 1 - text-logger-slf4j/build.gradle.kts | 1 - .../logger/slf4j/ComponentLoggerProvider.java | 1 + .../slf4j/WrappingComponentLoggerImpl.java | 1169 +++++++++++++++-- .../logger/slf4j/ComponentLoggerTest.java | 9 + 5 files changed, 1076 insertions(+), 105 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 77f47173a..1f67e61a7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -32,7 +32,6 @@ configurate-v4 = "org.spongepowered:configurate-core:4.1.2" # text-logger-slf4j slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } -slf4j-ext = { module = "org.slf4j:slf4j-ext", version.ref = "slf4j" } slf4jtest = "com.github.valfirst:slf4j-test:2.6.1" # Specific versions are needed for different SLF4J versions # text-serializer-gson diff --git a/text-logger-slf4j/build.gradle.kts b/text-logger-slf4j/build.gradle.kts index c2705f3a3..34e7b2478 100644 --- a/text-logger-slf4j/build.gradle.kts +++ b/text-logger-slf4j/build.gradle.kts @@ -5,7 +5,6 @@ plugins { dependencies { api(projects.adventureApi) api(libs.slf4j) - implementation(libs.slf4j.ext) testImplementation(libs.slf4jtest) } diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java index ba97cfd36..4e9f50520 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerProvider.java @@ -34,6 +34,7 @@ * * @since 4.11.0 */ +@ApiStatus.Internal // SPI for platform use only public interface ComponentLoggerProvider { /** * Create a component logger for the provided logger name. diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java index 05cbbb33f..86ae91ac4 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java @@ -30,17 +30,19 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.Marker; -import org.slf4j.ext.LoggerWrapper; import org.slf4j.spi.LocationAwareLogger; -final class WrappingComponentLoggerImpl extends LoggerWrapper implements ComponentLogger { +final class WrappingComponentLoggerImpl implements ComponentLogger { private static final String FQCN = WrappingComponentLoggerImpl.class.getName(); + private final Logger logger; + private final boolean isLocationAware; private final Function serializer; WrappingComponentLoggerImpl(final Logger backing, final Function serializer) { - super(backing, LoggerWrapper.class.getName()); this.serializer = serializer; + this.logger = backing; + this.isLocationAware = backing instanceof LocationAwareLogger; } private String serialize(final Component input) { @@ -71,11 +73,972 @@ private Object[] maybeSerialize(final @Nullable Object@NotNull... args) { return args; } + // Basic methods, plain delegation + + @Override + public String getName() { + return this.logger.getName(); + } + + @Override + public boolean isTraceEnabled() { + return this.logger.isTraceEnabled(); + } + + @Override + public boolean isTraceEnabled(final Marker marker) { + return this.logger.isTraceEnabled(marker); + } + + @Override + public boolean isDebugEnabled() { + return this.logger.isDebugEnabled(); + } + + @Override + public boolean isDebugEnabled(final Marker marker) { + return this.logger.isDebugEnabled(marker); + } + + @Override + public boolean isInfoEnabled() { + return this.logger.isInfoEnabled(); + } + + @Override + public boolean isInfoEnabled(final Marker marker) { + return this.logger.isInfoEnabled(marker); + } + + @Override + public boolean isWarnEnabled() { + return this.logger.isWarnEnabled(); + } + + @Override + public boolean isWarnEnabled(final Marker marker) { + return this.logger.isWarnEnabled(marker); + } + + @Override + public boolean isErrorEnabled() { + return this.logger.isErrorEnabled(); + } + + @Override + public boolean isErrorEnabled(final Marker marker) { + return this.logger.isErrorEnabled(marker); + } + + // Standard string methods, to process potential Component arguments + + @Override + public void trace(final @NotNull String format) { + if (!this.isTraceEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + null, + null + ); + } else { + this.logger.trace(format); + } + } + + @Override + public void trace(final @NotNull String format, final @Nullable Object arg) { + if (!this.isTraceEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.trace(format, this.maybeSerialize(arg)); + } + } + + @Override + public void trace(final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isTraceEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.trace(format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void trace(final @NotNull String format, final @Nullable Object @NotNull... arguments) { + if (!this.isTraceEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + this.maybeSerialize(arguments), + null + ); + } else { + this.logger.trace(format, this.maybeSerialize(arguments)); + } + } + + @Override + public void trace(final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isTraceEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.TRACE_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.trace(msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void trace(final @NotNull Marker marker, final @NotNull String msg) { + if (!this.isTraceEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + msg, + null, + null + ); + } else { + this.logger.trace(marker, msg); + } + } + + @Override + public void trace(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg) { + if (!this.isTraceEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.trace(marker, format, this.maybeSerialize(arg)); + } + } + + @Override + public void trace(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isTraceEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.trace(marker, format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void trace(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object @NotNull... argArray) { + if (!this.isTraceEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + format, + this.maybeSerialize(argArray), + null + ); + } else { + this.logger.trace(marker, format, this.maybeSerialize(argArray)); + } + } + + @Override + public void trace(final @NotNull Marker marker, final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isTraceEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.TRACE_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.trace(marker, msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void debug(final @NotNull String format) { + if (!this.isDebugEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + null, + null + ); + } else { + this.logger.debug(format); + } + } + + @Override + public void debug(final @NotNull String format, final @Nullable Object arg) { + if (!this.isDebugEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.debug(format, this.maybeSerialize(arg)); + } + } + + @Override + public void debug(final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isDebugEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.debug(format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void debug(final @NotNull String format, final @Nullable Object @NotNull... arguments) { + if (!this.isDebugEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + this.maybeSerialize(arguments), + null + ); + } else { + this.logger.debug(format, this.maybeSerialize(arguments)); + } + } + + @Override + public void debug(final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isDebugEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.DEBUG_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.debug(msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void debug(final @NotNull Marker marker, final @NotNull String msg) { + if (!this.isDebugEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + msg, + null, + null + ); + } else { + this.logger.debug(marker, msg); + } + } + + @Override + public void debug(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg) { + if (!this.isDebugEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.debug(marker, format, this.maybeSerialize(arg)); + } + } + + @Override + public void debug(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isDebugEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.debug(marker, format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void debug(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object @NotNull... argArray) { + if (!this.isDebugEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + format, + this.maybeSerialize(argArray), + null + ); + } else { + this.logger.debug(marker, format, this.maybeSerialize(argArray)); + } + } + + @Override + public void debug(final @NotNull Marker marker, final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isDebugEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.DEBUG_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.debug(marker, msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void info(final @NotNull String format) { + if (!this.isInfoEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + format, + null, + null + ); + } else { + this.logger.info(format); + } + } + + @Override + public void info(final @NotNull String format, final @Nullable Object arg) { + if (!this.isInfoEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.info(format, this.maybeSerialize(arg)); + } + } + + @Override + public void info(final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isInfoEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.info(format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void info(final @NotNull String format, final @Nullable Object @NotNull... arguments) { + if (!this.isInfoEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + format, + this.maybeSerialize(arguments), + null + ); + } else { + this.logger.info(format, this.maybeSerialize(arguments)); + } + } + + @Override + public void info(final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isInfoEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.INFO_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.info(msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void info(final @NotNull Marker marker, final @NotNull String msg) { + if (!this.isInfoEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + msg, + null, + null + ); + } else { + this.logger.info(marker, msg); + } + } + + @Override + public void info(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg) { + if (!this.isInfoEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.info(marker, format, this.maybeSerialize(arg)); + } + } + + @Override + public void info(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isInfoEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.info(marker, format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void info(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object @NotNull... argArray) { + if (!this.isInfoEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + format, + this.maybeSerialize(argArray), + null + ); + } else { + this.logger.info(marker, format, this.maybeSerialize(argArray)); + } + } + + @Override + public void info(final @NotNull Marker marker, final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isInfoEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.INFO_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.info(marker, msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void warn(final @NotNull String format) { + if (!this.isWarnEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + format, + null, + null + ); + } else { + this.logger.warn(format); + } + } + + @Override + public void warn(final @NotNull String format, final @Nullable Object arg) { + if (!this.isWarnEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.warn(format, this.maybeSerialize(arg)); + } + } + + @Override + public void warn(final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isWarnEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.warn(format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void warn(final @NotNull String format, final @Nullable Object @NotNull... arguments) { + if (!this.isWarnEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + format, + this.maybeSerialize(arguments), + null + ); + } else { + this.logger.warn(format, this.maybeSerialize(arguments)); + } + } + + @Override + public void warn(final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isWarnEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.WARN_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.warn(msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void warn(final @NotNull Marker marker, final @NotNull String msg) { + if (!this.isWarnEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + msg, + null, + null + ); + } else { + this.logger.warn(marker, msg); + } + } + + @Override + public void warn(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg) { + if (!this.isWarnEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.warn(marker, format, this.maybeSerialize(arg)); + } + } + + @Override + public void warn(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isWarnEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.warn(marker, format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void warn(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object @NotNull... argArray) { + if (!this.isWarnEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + format, + this.maybeSerialize(argArray), + null + ); + } else { + this.logger.warn(marker, format, this.maybeSerialize(argArray)); + } + } + + @Override + public void warn(final @NotNull Marker marker, final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isWarnEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.WARN_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.warn(marker, msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void error(final @NotNull String format) { + if (!this.isErrorEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + null, + null + ); + } else { + this.logger.error(format); + } + } + + @Override + public void error(final @NotNull String format, final @Nullable Object arg) { + if (!this.isErrorEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.error(format, this.maybeSerialize(arg)); + } + } + + @Override + public void error(final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isErrorEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.error(format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void error(final @NotNull String format, final @Nullable Object @NotNull... arguments) { + if (!this.isErrorEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + this.maybeSerialize(arguments), + null + ); + } else { + this.logger.error(format, this.maybeSerialize(arguments)); + } + } + + @Override + public void error(final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isErrorEnabled()) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + null, + FQCN, + LocationAwareLogger.ERROR_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.error(msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + @Override + public void error(final @NotNull Marker marker, final @NotNull String msg) { + if (!this.isErrorEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + msg, + null, + null + ); + } else { + this.logger.error(marker, msg); + } + } + + @Override + public void error(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg) { + if (!this.isErrorEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + new Object[] {this.maybeSerialize(arg)}, + null + ); + } else { + this.logger.error(marker, format, this.maybeSerialize(arg)); + } + } + + @Override + public void error(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object arg1, final @Nullable Object arg2) { + if (!this.isErrorEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + new Object[] {this.maybeSerialize(arg1), this.maybeSerialize(arg2)}, + null + ); + } else { + this.logger.error(marker, format, this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + } + } + + @Override + public void error(final @NotNull Marker marker, final @NotNull String format, final @Nullable Object @NotNull... argArray) { + if (!this.isErrorEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + format, + this.maybeSerialize(argArray), + null + ); + } else { + this.logger.error(marker, format, this.maybeSerialize(argArray)); + } + } + + @Override + public void error(final @NotNull Marker marker, final @NotNull String msg, final @Nullable Throwable t) { + if (!this.isErrorEnabled(marker)) return; + + if (this.isLocationAware) { + ((LocationAwareLogger) this.logger).log( + marker, + FQCN, + LocationAwareLogger.ERROR_INT, + msg, + null, + UnpackedComponentThrowable.unpack(t, this.serializer) + ); + } else { + this.logger.error(marker, msg, UnpackedComponentThrowable.unpack(t, this.serializer)); + } + } + + // Component-primary methods + @Override public void trace(final @NotNull Component format) { if (!this.isTraceEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -85,7 +1048,7 @@ public void trace(final @NotNull Component format) { null ); } else { - this.trace(this.serialize(format)); + this.logger.trace(this.serialize(format)); } } @@ -93,7 +1056,7 @@ public void trace(final @NotNull Component format) { public void trace(final @NotNull Component format, final @Nullable Object arg) { if (!this.isTraceEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -103,7 +1066,7 @@ public void trace(final @NotNull Component format, final @Nullable Object arg) { null ); } else { - this.trace(this.serialize(format), this.maybeSerialize(arg)); + this.logger.trace(this.serialize(format), this.maybeSerialize(arg)); } } @@ -111,7 +1074,7 @@ public void trace(final @NotNull Component format, final @Nullable Object arg) { public void trace(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isTraceEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -121,7 +1084,7 @@ public void trace(final @NotNull Component format, final @Nullable Object arg1, null ); } else { - this.trace(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.trace(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -129,7 +1092,7 @@ public void trace(final @NotNull Component format, final @Nullable Object arg1, public void trace(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isTraceEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -139,7 +1102,7 @@ public void trace(final @NotNull Component format, final @Nullable Object @NotNu null ); } else { - this.trace(this.serialize(format), this.maybeSerialize(arguments)); + this.logger.trace(this.serialize(format), this.maybeSerialize(arguments)); } } @@ -147,7 +1110,7 @@ public void trace(final @NotNull Component format, final @Nullable Object @NotNu public void trace(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isTraceEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -157,7 +1120,7 @@ public void trace(final @NotNull Component msg, final @Nullable Throwable t) { UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.trace(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.trace(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -165,7 +1128,7 @@ public void trace(final @NotNull Component msg, final @Nullable Throwable t) { public void trace(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isTraceEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -175,7 +1138,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component msg) { null ); } else { - this.trace(marker, this.serialize(msg)); + this.logger.trace(marker, this.serialize(msg)); } } @@ -183,7 +1146,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component msg) { public void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isTraceEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -193,7 +1156,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.trace(marker, this.serialize(format), this.maybeSerialize(arg)); + this.logger.trace(marker, this.serialize(format), this.maybeSerialize(arg)); } } @@ -201,7 +1164,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component format, public void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isTraceEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -211,7 +1174,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.trace(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.trace(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -219,7 +1182,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component format, public void trace(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isTraceEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -229,7 +1192,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.trace(marker, this.serialize(format), this.maybeSerialize(argArray)); + this.logger.trace(marker, this.serialize(format), this.maybeSerialize(argArray)); } } @@ -237,7 +1200,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component format, public void trace(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isTraceEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -247,7 +1210,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component msg, fi UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.trace(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.trace(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -255,7 +1218,7 @@ public void trace(final @NotNull Marker marker, final @NotNull Component msg, fi public void debug(final @NotNull Component format) { if (!this.isDebugEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -265,7 +1228,7 @@ public void debug(final @NotNull Component format) { null ); } else { - this.debug(this.serialize(format)); + this.logger.debug(this.serialize(format)); } } @@ -273,7 +1236,7 @@ public void debug(final @NotNull Component format) { public void debug(final @NotNull Component format, final @Nullable Object arg) { if (!this.isDebugEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -283,7 +1246,7 @@ public void debug(final @NotNull Component format, final @Nullable Object arg) { null ); } else { - this.debug(this.serialize(format), this.maybeSerialize(arg)); + this.logger.debug(this.serialize(format), this.maybeSerialize(arg)); } } @@ -291,7 +1254,7 @@ public void debug(final @NotNull Component format, final @Nullable Object arg) { public void debug(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isDebugEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -301,7 +1264,7 @@ public void debug(final @NotNull Component format, final @Nullable Object arg1, null ); } else { - this.debug(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.debug(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -309,7 +1272,7 @@ public void debug(final @NotNull Component format, final @Nullable Object arg1, public void debug(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isDebugEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -319,7 +1282,7 @@ public void debug(final @NotNull Component format, final @Nullable Object @NotNu null ); } else { - this.debug(this.serialize(format), this.maybeSerialize(arguments)); + this.logger.debug(this.serialize(format), this.maybeSerialize(arguments)); } } @@ -327,7 +1290,7 @@ public void debug(final @NotNull Component format, final @Nullable Object @NotNu public void debug(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isDebugEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -337,7 +1300,7 @@ public void debug(final @NotNull Component msg, final @Nullable Throwable t) { UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.debug(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.debug(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -345,7 +1308,7 @@ public void debug(final @NotNull Component msg, final @Nullable Throwable t) { public void debug(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isDebugEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -355,7 +1318,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component msg) { null ); } else { - this.debug(marker, this.serialize(msg)); + this.logger.debug(marker, this.serialize(msg)); } } @@ -363,7 +1326,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component msg) { public void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isDebugEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -373,7 +1336,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.debug(marker, this.serialize(format), this.maybeSerialize(arg)); + this.logger.debug(marker, this.serialize(format), this.maybeSerialize(arg)); } } @@ -381,7 +1344,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component format, public void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isDebugEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -391,7 +1354,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.debug(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.debug(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -399,7 +1362,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component format, public void debug(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isDebugEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -409,7 +1372,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.debug(marker, this.serialize(format), this.maybeSerialize(argArray)); + this.logger.debug(marker, this.serialize(format), this.maybeSerialize(argArray)); } } @@ -417,7 +1380,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component format, public void debug(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isDebugEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -427,7 +1390,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component msg, fi UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.debug(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.debug(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -435,7 +1398,7 @@ public void debug(final @NotNull Marker marker, final @NotNull Component msg, fi public void info(final @NotNull Component format) { if (!this.isInfoEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -445,7 +1408,7 @@ public void info(final @NotNull Component format) { null ); } else { - this.info(this.serialize(format)); + this.logger.info(this.serialize(format)); } } @@ -453,7 +1416,7 @@ public void info(final @NotNull Component format) { public void info(final @NotNull Component format, final @Nullable Object arg) { if (!this.isInfoEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -463,7 +1426,7 @@ public void info(final @NotNull Component format, final @Nullable Object arg) { null ); } else { - this.info(this.serialize(format), this.maybeSerialize(arg)); + this.logger.info(this.serialize(format), this.maybeSerialize(arg)); } } @@ -471,7 +1434,7 @@ public void info(final @NotNull Component format, final @Nullable Object arg) { public void info(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isInfoEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -481,7 +1444,7 @@ public void info(final @NotNull Component format, final @Nullable Object arg1, f null ); } else { - this.info(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.info(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -489,7 +1452,7 @@ public void info(final @NotNull Component format, final @Nullable Object arg1, f public void info(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isInfoEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -499,7 +1462,7 @@ public void info(final @NotNull Component format, final @Nullable Object @NotNul null ); } else { - this.info(this.serialize(format), this.maybeSerialize(arguments)); + this.logger.info(this.serialize(format), this.maybeSerialize(arguments)); } } @@ -507,7 +1470,7 @@ public void info(final @NotNull Component format, final @Nullable Object @NotNul public void info(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isInfoEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -517,7 +1480,7 @@ public void info(final @NotNull Component msg, final @Nullable Throwable t) { UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.info(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.info(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -525,7 +1488,7 @@ public void info(final @NotNull Component msg, final @Nullable Throwable t) { public void info(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isInfoEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -535,7 +1498,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component msg) { null ); } else { - this.info(marker, this.serialize(msg)); + this.logger.info(marker, this.serialize(msg)); } } @@ -543,7 +1506,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component msg) { public void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isInfoEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -553,7 +1516,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.info(marker, this.serialize(format), this.maybeSerialize(arg)); + this.logger.info(marker, this.serialize(format), this.maybeSerialize(arg)); } } @@ -561,7 +1524,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component format, public void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isInfoEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -571,7 +1534,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.info(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.info(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -579,7 +1542,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component format, public void info(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isInfoEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -589,7 +1552,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.info(marker, this.serialize(format), this.maybeSerialize(argArray)); + this.logger.info(marker, this.serialize(format), this.maybeSerialize(argArray)); } } @@ -597,7 +1560,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component format, public void info(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isInfoEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -607,7 +1570,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component msg, fin UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.info(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.info(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -615,7 +1578,7 @@ public void info(final @NotNull Marker marker, final @NotNull Component msg, fin public void warn(final @NotNull Component format) { if (!this.isWarnEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -625,7 +1588,7 @@ public void warn(final @NotNull Component format) { null ); } else { - this.warn(this.serialize(format)); + this.logger.warn(this.serialize(format)); } } @@ -633,7 +1596,7 @@ public void warn(final @NotNull Component format) { public void warn(final @NotNull Component format, final @Nullable Object arg) { if (!this.isWarnEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -643,7 +1606,7 @@ public void warn(final @NotNull Component format, final @Nullable Object arg) { null ); } else { - this.warn(this.serialize(format), this.maybeSerialize(arg)); + this.logger.warn(this.serialize(format), this.maybeSerialize(arg)); } } @@ -651,7 +1614,7 @@ public void warn(final @NotNull Component format, final @Nullable Object arg) { public void warn(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isWarnEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -661,7 +1624,7 @@ public void warn(final @NotNull Component format, final @Nullable Object arg1, f null ); } else { - this.warn(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.warn(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -669,7 +1632,7 @@ public void warn(final @NotNull Component format, final @Nullable Object arg1, f public void warn(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isWarnEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -679,7 +1642,7 @@ public void warn(final @NotNull Component format, final @Nullable Object @NotNul null ); } else { - this.warn(this.serialize(format), this.maybeSerialize(arguments)); + this.logger.warn(this.serialize(format), this.maybeSerialize(arguments)); } } @@ -687,7 +1650,7 @@ public void warn(final @NotNull Component format, final @Nullable Object @NotNul public void warn(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isWarnEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -697,7 +1660,7 @@ public void warn(final @NotNull Component msg, final @Nullable Throwable t) { UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.warn(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.warn(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -705,7 +1668,7 @@ public void warn(final @NotNull Component msg, final @Nullable Throwable t) { public void warn(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isWarnEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -715,7 +1678,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component msg) { null ); } else { - this.warn(marker, this.serialize(msg)); + this.logger.warn(marker, this.serialize(msg)); } } @@ -723,7 +1686,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component msg) { public void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isWarnEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -733,7 +1696,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.warn(marker, this.serialize(format), this.maybeSerialize(arg)); + this.logger.warn(marker, this.serialize(format), this.maybeSerialize(arg)); } } @@ -741,7 +1704,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component format, public void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isWarnEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -751,7 +1714,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.warn(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.warn(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -759,7 +1722,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component format, public void warn(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isWarnEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -769,7 +1732,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.warn(marker, this.serialize(format), this.maybeSerialize(argArray)); + this.logger.warn(marker, this.serialize(format), this.maybeSerialize(argArray)); } } @@ -777,7 +1740,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component format, public void warn(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isWarnEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -787,7 +1750,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component msg, fin UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.warn(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.warn(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -795,7 +1758,7 @@ public void warn(final @NotNull Marker marker, final @NotNull Component msg, fin public void error(final @NotNull Component format) { if (!this.isErrorEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -805,7 +1768,7 @@ public void error(final @NotNull Component format) { null ); } else { - this.error(this.serialize(format)); + this.logger.error(this.serialize(format)); } } @@ -813,7 +1776,7 @@ public void error(final @NotNull Component format) { public void error(final @NotNull Component format, final @Nullable Object arg) { if (!this.isErrorEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -823,7 +1786,7 @@ public void error(final @NotNull Component format, final @Nullable Object arg) { null ); } else { - this.error(this.serialize(format), this.maybeSerialize(arg)); + this.logger.error(this.serialize(format), this.maybeSerialize(arg)); } } @@ -831,7 +1794,7 @@ public void error(final @NotNull Component format, final @Nullable Object arg) { public void error(final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isErrorEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -841,7 +1804,7 @@ public void error(final @NotNull Component format, final @Nullable Object arg1, null ); } else { - this.error(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.error(this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -849,7 +1812,7 @@ public void error(final @NotNull Component format, final @Nullable Object arg1, public void error(final @NotNull Component format, final @Nullable Object @NotNull... arguments) { if (!this.isErrorEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -859,7 +1822,7 @@ public void error(final @NotNull Component format, final @Nullable Object @NotNu null ); } else { - this.error(this.serialize(format), this.maybeSerialize(arguments)); + this.logger.error(this.serialize(format), this.maybeSerialize(arguments)); } } @@ -867,7 +1830,7 @@ public void error(final @NotNull Component format, final @Nullable Object @NotNu public void error(final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isErrorEnabled()) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( null, FQCN, @@ -877,7 +1840,7 @@ public void error(final @NotNull Component msg, final @Nullable Throwable t) { UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.error(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.error(this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } @@ -885,7 +1848,7 @@ public void error(final @NotNull Component msg, final @Nullable Throwable t) { public void error(final @NotNull Marker marker, final @NotNull Component msg) { if (!this.isErrorEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -895,7 +1858,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component msg) { null ); } else { - this.error(marker, this.serialize(msg)); + this.logger.error(marker, this.serialize(msg)); } } @@ -903,7 +1866,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component msg) { public void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg) { if (!this.isErrorEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -913,7 +1876,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.error(marker, this.serialize(format), this.maybeSerialize(arg)); + this.logger.error(marker, this.serialize(format), this.maybeSerialize(arg)); } } @@ -921,7 +1884,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component format, public void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object arg1, final @Nullable Object arg2) { if (!this.isErrorEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -931,7 +1894,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.error(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); + this.logger.error(marker, this.serialize(format), this.maybeSerialize(arg1), this.maybeSerialize(arg2)); } } @@ -939,7 +1902,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component format, public void error(final @NotNull Marker marker, final @NotNull Component format, final @Nullable Object @NotNull... argArray) { if (!this.isErrorEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -949,7 +1912,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component format, null ); } else { - this.error(marker, this.serialize(format), this.maybeSerialize(argArray)); + this.logger.error(marker, this.serialize(format), this.maybeSerialize(argArray)); } } @@ -957,7 +1920,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component format, public void error(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t) { if (!this.isErrorEnabled(marker)) return; - if (this.instanceofLAL) { + if (this.isLocationAware) { ((LocationAwareLogger) this.logger).log( marker, FQCN, @@ -967,7 +1930,7 @@ public void error(final @NotNull Marker marker, final @NotNull Component msg, fi UnpackedComponentThrowable.unpack(t, this.serializer) ); } else { - this.error(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); + this.logger.error(marker, this.serialize(msg), UnpackedComponentThrowable.unpack(t, this.serializer)); } } } diff --git a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java index cffb91fb7..ba7b759e2 100644 --- a/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java +++ b/text-logger-slf4j/src/test/java/net/kyori/adventure/text/logger/slf4j/ComponentLoggerTest.java @@ -147,4 +147,13 @@ void testWithMarker() { ImmutableList.of(LoggingEvent.info(MARKED, "meow :3")) ); } + + @Test + void testComponentAsArgToPlainLog() { + this.makeLogger().info("Hello {}", Component.text("friend")); + assertEquals( + LOGGER.getLoggingEvents(), + ImmutableList.of(LoggingEvent.info("Hello {}", "friend")) + ); + } } From 6d69f388d157fa3db71aacad62021466465afbba Mon Sep 17 00:00:00 2001 From: zml Date: Sat, 21 May 2022 20:26:06 -0700 Subject: [PATCH 8/9] text-logger-slf4j: Defensively copy any passed-in argument arrays --- .../slf4j/WrappingComponentLoggerImpl.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java index 86ae91ac4..4d13594fd 100644 --- a/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java +++ b/text-logger-slf4j/src/main/java/net/kyori/adventure/text/logger/slf4j/WrappingComponentLoggerImpl.java @@ -23,6 +23,7 @@ */ package net.kyori.adventure.text.logger.slf4j; +import java.util.Arrays; import java.util.function.Function; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; @@ -60,17 +61,24 @@ private Object maybeSerialize(final @Nullable Object input) { } private Object[] maybeSerialize(final @Nullable Object@NotNull... args) { - for (int i = 0; i < args.length; i++) { - if (args[i] instanceof ComponentLike) { - args[i] = this.serialize(((ComponentLike) args[i]).asComponent()); + Object[] writable = args; + for (int i = 0; i < writable.length; i++) { + if (writable[i] instanceof ComponentLike) { + if (writable == args) { + writable = Arrays.copyOf(args, args.length); + } + writable[i] = this.serialize(((ComponentLike) writable[i]).asComponent()); } } - if (args.length > 0 && args[args.length - 1] instanceof Throwable) { - args[args.length - 1] = UnpackedComponentThrowable.unpack((Throwable) args[args.length - 1], this.serializer); + if (writable.length > 0 && writable[writable.length - 1] instanceof Throwable) { + if (writable == args) { + writable = Arrays.copyOf(args, args.length); + } + writable[writable.length - 1] = UnpackedComponentThrowable.unpack((Throwable) writable[writable.length - 1], this.serializer); } - return args; + return writable; } // Basic methods, plain delegation From 916a596363227829c22160b3eec72201d8f28bb6 Mon Sep 17 00:00:00 2001 From: zml Date: Sat, 21 May 2022 20:50:03 -0700 Subject: [PATCH 9/9] text-logger-slf4j: Add Automatic-Module-Name to manifest --- text-logger-slf4j/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/text-logger-slf4j/build.gradle.kts b/text-logger-slf4j/build.gradle.kts index 34e7b2478..f5c0c54e1 100644 --- a/text-logger-slf4j/build.gradle.kts +++ b/text-logger-slf4j/build.gradle.kts @@ -14,6 +14,8 @@ sourceSets.main { } } +applyJarMetadata("net.kyori.adventure.text.logger.slf4j") + eclipse { // Make sure slf4j doesn't end up on the module path until we are actually a module classpath.file.whenMerged {