Skip to content

Commit

Permalink
Merge pull request #826 from KyoriPowered/feature/slf4j-2
Browse files Browse the repository at this point in the history
text-logger-slf4j: Add wrapper for fluent API
  • Loading branch information
zml2008 committed Nov 9, 2022
2 parents d0dca11 + c88ace5 commit 59100a9
Show file tree
Hide file tree
Showing 10 changed files with 677 additions and 4 deletions.
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jmh = "1.35"
jmhPlugin = "0.6.8"
junit = "5.9.1"
mockito = "4.5.1"
slf4j = "1.7.36"
slf4j = "2.0.1"
truth = "1.1.3"

[libraries]
Expand All @@ -32,6 +32,7 @@ configurate-v4 = "org.spongepowered:configurate-core:4.1.2"

# text-logger-slf4j
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
slf4jRuntime = { module = "org.slf4j:slf4j-api", version = "1.7.36"}
slf4jtest = "com.github.valfirst:slf4j-test:2.6.1" # Specific versions are needed for different SLF4J versions

# text-serializer-gson
Expand Down
3 changes: 2 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"local>KyoriPowered/.github:renovate-config"
],
"ignoreDeps": [
"com.google.code.gson:gson"
"com.google.code.gson:gson",
"org.slf4j:slf4j-api"
],
"packageRules": [
{
Expand Down
19 changes: 18 additions & 1 deletion text-logger-slf4j/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@ plugins {
id("adventure.common-conventions")
}

val exposedVersion by configurations.creating {
}

configurations {
apiElements {
extendsFrom(exposedVersion)
}
runtimeClasspath {
extendsFrom(exposedVersion)
}
runtimeElements {
extendsFrom(exposedVersion)
}
}

dependencies {
api(projects.adventureApi)
api(libs.slf4j)
compileOnly(libs.slf4j)
exposedVersion(libs.slf4jRuntime)
testImplementation(libs.slf4jtest)
testImplementation(libs.slf4j)
}

sourceSets.main {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.event.Level;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -198,6 +199,19 @@ public interface ComponentLogger extends Logger {
*/
void trace(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t);

/**
* Entry point for fluent-logging for {@link Level#TRACE} level.
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @return LoggingEventBuilder instance as appropriate for level TRACE
* @since 4.12.0
*/
@Override
default ComponentLoggingEventBuilder atTrace() {
return this.atLevel(Level.TRACE);
}

/**
* Log a message at the DEBUG level.
*
Expand Down Expand Up @@ -316,6 +330,19 @@ public interface ComponentLogger extends Logger {
*/
void debug(final @NotNull Marker marker, final @NotNull Component msg, final @Nullable Throwable t);

/**
* Entry point for fluent-logging for {@link Level#DEBUG} level.
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @return LoggingEventBuilder instance as appropriate for level DEBUG
* @since 4.12.0
*/
@Override
default ComponentLoggingEventBuilder atDebug() {
return this.atLevel(Level.DEBUG);
}

/**
* Log a message at the INFO level.
*
Expand Down Expand Up @@ -434,6 +461,19 @@ public interface ComponentLogger extends Logger {
*/
void info(final @NotNull Marker marker, final @NotNull Component msg, final @NotNull Throwable t);

/**
* Entry point for fluent-logging for {@link Level#INFO} level.
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @return LoggingEventBuilder instance as appropriate for level INFO
* @since 4.12.0
*/
@Override
default ComponentLoggingEventBuilder atInfo() {
return this.atLevel(Level.INFO);
}

/**
* Log a message at the WARN level.
*
Expand Down Expand Up @@ -552,6 +592,19 @@ public interface ComponentLogger extends Logger {
*/
void warn(final @NotNull Marker marker, final @NotNull Component msg, final @NotNull Throwable t);

/**
* Entry point for fluent-logging for {@link Level#WARN} level.
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @return LoggingEventBuilder instance as appropriate for level WARN
* @since 4.12.0
*/
@Override
default ComponentLoggingEventBuilder atWarn() {
return this.atLevel(Level.WARN);
}

/**
* Log a message at the ERROR level.
*
Expand Down Expand Up @@ -670,4 +723,43 @@ public interface ComponentLogger extends Logger {
* @since 4.11.0
*/
void error(final @NotNull Marker marker, final @NotNull Component msg, final @NotNull Throwable t);

/**
* Entry point for fluent-logging for {@link Level#ERROR} level.
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @return LoggingEventBuilder instance as appropriate for level ERROR
* @since 4.12.0
*/
@Override
default @NotNull ComponentLoggingEventBuilder atError() {
return this.atLevel(Level.ERROR);
}

/**
* Unconditionally create a new logging event builder.
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @param level desired level for the event builder
* @return a new event builder
* @since 4.12.0
*/
@Override
@NotNull ComponentLoggingEventBuilder makeLoggingEventBuilder(final @NotNull Level level);

/**
* Create the appropriate builder for the supplied level.
*
* <p>This may be a no-op if the passed level is disabled.</p>
*
* <p><b>Warning:</b> This method is only available when running with SLF4J 2.0+</p>
*
* @param level desired level for the event builder
* @return a new event builder
* @since 4.12.0
*/
@Override
@NotNull ComponentLoggingEventBuilder atLevel(final @NotNull Level level);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* 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.Supplier;
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.Marker;
import org.slf4j.helpers.CheckReturnValue;
import org.slf4j.spi.LoggingEventBuilder;

/**
* A builder for logging events that accepts {@link Component} messages and arguments.
*
* <p>Note: This class is only available when used in conjunction with SLF4J 2.0.0 or newer.</p>
*
* @since 4.12.0
*/
public interface ComponentLoggingEventBuilder extends LoggingEventBuilder {

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder setCause(final @Nullable Throwable cause);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder addMarker(final @NotNull Marker marker);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder addArgument(final @Nullable Object p);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder addArgument(final @Nullable Supplier<?> objectSupplier);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder addKeyValue(final @Nullable String key, final @Nullable Object value);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder addKeyValue(final @Nullable String key, final Supplier<Object> valueSupplier);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder setMessage(final @Nullable String message);

/**
* Set the message to be logged for this event.
*
* @param message the message
* @return this builder
* @since 4.12.0
*/
@SuppressWarnings("checkstyle:MethodName") // match overloads
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder setMessage(final @Nullable ComponentLike message);

@Override
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder setMessage(final @NotNull Supplier<String> messageSupplier);

/**
* Set the message supplier to be logged for this event.
*
* @param messageSupplier the message supplier
* @return this builder
* @since 4.12.0
*/
@SuppressWarnings("checkstyle:MethodName") // match overloads
@CheckReturnValue
@NotNull ComponentLoggingEventBuilder setComponentMessage(final @NotNull Supplier<? extends ComponentLike> messageSupplier);

/**
* Set the message and publish this logging event.
*
* @param message the message
* @see #log()
* @since 4.12.0
*/
void log(final @Nullable ComponentLike message);

/**
* Set the message with one argument and publish this logging event.
*
* @param message the message
* @param arg the argument
* @see #log()
* @since 4.12.0
*/
void log(final @Nullable ComponentLike message, final @Nullable Object arg);

/**
* Set the message with two arguments and publish this logging event.
*
* @param message the message
* @param arg0 the first argument
* @param arg1 the second argument
* @see #log()
* @since 4.12.0
*/
void log(final @Nullable ComponentLike message, final @Nullable Object arg0, final @Nullable Object arg1);

/**
* Set the message with an array of arguments and publish this logging event.
*
* @param message the message
* @param args the arguments
* @see #log()
* @since 4.12.0
*/
void log(final @Nullable ComponentLike message, @Nullable Object@NotNull... args);

/**
* Set the message supplier and publish this logging event.
*
* @param messageSupplier the message supplier
* @see #log()
* @since 4.12.0
*/
void logComponent(final @NotNull Supplier<? extends @Nullable ComponentLike> messageSupplier);
}

0 comments on commit 59100a9

Please sign in to comment.