Paper/patches/api/0070-Allow-plugins-to-use-SLF4J-for-logging.patch
2024-04-28 02:05:52 +02:00

74 lines
3.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Minecrell <minecrell@minecrell.net>
Date: Thu, 21 Sep 2017 16:33:12 +0200
Subject: [PATCH] Allow plugins to use SLF4J for logging
SLF4J is a commonly used abstraction for various logging frameworks
such as java.util.logging (JUL) or Log4j. Currently, plugins are
required to do all their logging using the provided JUL logger.
This is annoying for plugins that target multiple platforms or when
using libraries that log messages using SLF4J.
Expose SLF4J as optional logging API for plugins, so they can use
it without having to shade it in the plugin and going through
several layers of logging abstraction.
diff --git a/build.gradle.kts b/build.gradle.kts
index 5ccdd695948d1d36173b0a4516cfe8494dd01b06..65e67b8726f1e19a6bcb1fe2f448e4ab68df11d1 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -12,6 +12,8 @@ java {
val annotationsVersion = "24.1.0"
val bungeeCordChatVersion = "1.20-R0.2"
val adventureVersion = "4.17.0-SNAPSHOT"
+val slf4jVersion = "2.0.9"
+val log4jVersion = "2.17.1"
val apiAndDocs: Configuration by configurations.creating {
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION))
@@ -47,6 +49,8 @@ dependencies {
apiAndDocs("net.kyori:adventure-text-serializer-legacy")
apiAndDocs("net.kyori:adventure-text-serializer-plain")
apiAndDocs("net.kyori:adventure-text-logger-slf4j")
+ api("org.apache.logging.log4j:log4j-api:$log4jVersion")
+ api("org.slf4j:slf4j-api:$slf4jVersion")
implementation("org.ow2.asm:asm:9.7")
implementation("org.ow2.asm:asm-commons:9.7")
@@ -137,6 +141,8 @@ tasks.withType<Javadoc> {
//"https://jd.advntr.dev/text-serializer-legacy/$adventureVersion/",
//"https://jd.advntr.dev/text-serializer-plain/$adventureVersion/",
//"https://jd.advntr.dev/text-logger-slf4j/$adventureVersion/",
+ "https://javadoc.io/doc/org.slf4j/slf4j-api/$slf4jVersion/",
+ "https://javadoc.io/doc/org.apache.logging.log4j/log4j-api/$log4jVersion/",
// Paper end
"https://javadoc.io/doc/org.apache.maven.resolver/maven-resolver-api/1.7.3", // Paper
)
diff --git a/src/main/java/org/bukkit/plugin/Plugin.java b/src/main/java/org/bukkit/plugin/Plugin.java
index 8c76716249e44ed8bf6be94c1f5c7b6d9bb35be2..4eb639fbb46a0848be207149ea433455550fae1c 100644
--- a/src/main/java/org/bukkit/plugin/Plugin.java
+++ b/src/main/java/org/bukkit/plugin/Plugin.java
@@ -198,6 +198,22 @@ public interface Plugin extends TabExecutor {
}
// Paper end
+ // Paper start - Add SLF4J/Log4J loggers
+ @NotNull
+ default org.slf4j.Logger getSLF4JLogger() {
+ return org.slf4j.LoggerFactory.getLogger(getLogger().getName());
+ }
+
+ /**
+ * @deprecated use {@link #getSLF4JLogger()}
+ */
+ @Deprecated
+ @NotNull
+ default org.apache.logging.log4j.Logger getLog4JLogger() {
+ return org.apache.logging.log4j.LogManager.getLogger(getLogger().getName());
+ }
+ // Paper end
+
/**
* Returns the name of the plugin.
* <p>