From cc70ca63284de5c577fc02ba6a657c9b898f4746 Mon Sep 17 00:00:00 2001 From: Apehum Date: Wed, 13 Mar 2024 23:40:23 +0800 Subject: [PATCH] feat: loggers --- api/common/build.gradle.kts | 3 -- .../src/main/kotlin/su/plo/slib/api/McLib.kt | 8 +++ .../su/plo/slib/api/logging/McLogger.kt | 52 +++++++++++++++++++ .../su/plo/slib/api/server/McServerLib.kt | 4 +- build.gradle.kts | 1 - .../su/plo/slib/bungee/BungeeProxyLib.kt | 9 +++- common/build.gradle.kts | 2 + .../kotlin/su/plo/slib/logging/JavaLogger.kt | 30 +++++++++++ .../kotlin/su/plo/slib/logging/Slf4jLogger.kt | 33 ++++++++++++ .../su/plo/slib/minestom/MinestomServerLib.kt | 5 ++ .../su/plo/slib/spigot/SpigotServerLib.kt | 12 +++++ .../su/plo/slib/velocity/VelocityProxyLib.kt | 5 ++ .../kotlin/su/plo/slib/mod/ModServerLib.kt | 5 ++ .../su/plo/slib/mod/logging/Log4jLogger.kt | 34 ++++++++++++ 14 files changed, 195 insertions(+), 8 deletions(-) create mode 100644 api/common/src/main/kotlin/su/plo/slib/api/logging/McLogger.kt create mode 100644 common/src/main/kotlin/su/plo/slib/logging/JavaLogger.kt create mode 100644 common/src/main/kotlin/su/plo/slib/logging/Slf4jLogger.kt create mode 100644 versions/src/main/kotlin/su/plo/slib/mod/logging/Log4jLogger.kt diff --git a/api/common/build.gradle.kts b/api/common/build.gradle.kts index 2e3ca6a..e69de29 100644 --- a/api/common/build.gradle.kts +++ b/api/common/build.gradle.kts @@ -1,3 +0,0 @@ -dependencies { - -} diff --git a/api/common/src/main/kotlin/su/plo/slib/api/McLib.kt b/api/common/src/main/kotlin/su/plo/slib/api/McLib.kt index 9fa4334..1477152 100644 --- a/api/common/src/main/kotlin/su/plo/slib/api/McLib.kt +++ b/api/common/src/main/kotlin/su/plo/slib/api/McLib.kt @@ -5,6 +5,7 @@ import su.plo.slib.api.chat.component.McTranslatableText import su.plo.slib.api.chat.converter.ServerTextConverter import su.plo.slib.api.command.McCommandManager import su.plo.slib.api.language.ServerTranslator +import su.plo.slib.api.logging.McLogger import su.plo.slib.api.permission.PermissionManager import java.io.File @@ -57,4 +58,11 @@ interface McLib { * @return The folder with plugins/mods configs. */ val configsFolder: File + + /** + * Creates a new logger with a specified name. + * + * @param name The name of the logger. + */ + fun createLogger(name: String): McLogger } diff --git a/api/common/src/main/kotlin/su/plo/slib/api/logging/McLogger.kt b/api/common/src/main/kotlin/su/plo/slib/api/logging/McLogger.kt new file mode 100644 index 0000000..1a7293b --- /dev/null +++ b/api/common/src/main/kotlin/su/plo/slib/api/logging/McLogger.kt @@ -0,0 +1,52 @@ +package su.plo.slib.api.logging + +/** + * Logger interface for wrapping different loggers. + */ +interface McLogger { + + /** + * Gets the name of the logger. + */ + fun getName(): String + + /** + * Log a message at the TRACE level. + * + * @param format The format string. + * @param arguments A list of arguments. + */ + fun trace(format: String, vararg arguments: Any) + + /** + * Log a message at the DEBUG level. + * + * @param format The format string. + * @param arguments A list of arguments. + */ + fun debug(format: String, vararg arguments: Any) + + /** + * Log a message at the INFO level. + * + * @param format The format string. + * @param arguments A list of arguments. + */ + fun info(format: String, vararg arguments: Any) + + /** + * Log a message at the WARN level. + * + * @param format The format string. + * @param arguments A list of arguments. + */ + fun warn(format: String, vararg arguments: Any) + + /** + * Log a message at the ERROR level. + * + * @param format The format string. + * @param arguments A list of arguments. + */ + fun error(format: String, vararg arguments: Any) +} diff --git a/api/server/src/main/kotlin/su/plo/slib/api/server/McServerLib.kt b/api/server/src/main/kotlin/su/plo/slib/api/server/McServerLib.kt index f7ff934..6c36c45 100644 --- a/api/server/src/main/kotlin/su/plo/slib/api/server/McServerLib.kt +++ b/api/server/src/main/kotlin/su/plo/slib/api/server/McServerLib.kt @@ -58,10 +58,10 @@ interface McServerLib : McLib { /** * Gets a player by their name. * - * @param name The name of the player. + * @param playerName The name of the player. * @return The player if found, otherwise `null`. */ - fun getPlayerByName(name: String): McServerPlayer? + fun getPlayerByName(playerName: String): McServerPlayer? /** * Gets a player by their unique identifier. diff --git a/build.gradle.kts b/build.gradle.kts index ebb73c6..8b2d2e3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -26,7 +26,6 @@ subprojects { implementation(rootProject.libs.kotlinx.coroutines) implementation(rootProject.libs.kotlinx.coroutines.jdk8) - implementation(rootProject.libs.slf4j) implementation(rootProject.libs.guava) } diff --git a/bungee/src/main/kotlin/su/plo/slib/bungee/BungeeProxyLib.kt b/bungee/src/main/kotlin/su/plo/slib/bungee/BungeeProxyLib.kt index 58d0d46..7c9ca3d 100644 --- a/bungee/src/main/kotlin/su/plo/slib/bungee/BungeeProxyLib.kt +++ b/bungee/src/main/kotlin/su/plo/slib/bungee/BungeeProxyLib.kt @@ -11,7 +11,7 @@ import net.md_5.bungee.api.plugin.Plugin import net.md_5.bungee.event.EventHandler import su.plo.slib.api.event.player.McPlayerJoinEvent import su.plo.slib.api.event.player.McPlayerQuitEvent -import su.plo.slib.api.language.ServerTranslator +import su.plo.slib.api.logging.McLogger import su.plo.slib.api.permission.PermissionManager import su.plo.slib.api.proxy.McProxyLib import su.plo.slib.api.proxy.event.command.McProxyCommandsRegisterEvent @@ -25,12 +25,13 @@ import su.plo.slib.bungee.permission.BungeePermissionSupplier import su.plo.slib.bungee.player.BungeeProxyPlayer import su.plo.slib.bungee.server.BungeeProxyServerInfo import su.plo.slib.language.ServerTranslatorFactory +import su.plo.slib.logging.JavaLogger import java.io.File import java.util.* import java.util.concurrent.ConcurrentHashMap class BungeeProxyLib( - loader: Plugin + private val loader: Plugin ) : McProxyLib, Listener { private val proxyServer = ProxyServer.getInstance() @@ -70,6 +71,10 @@ class BungeeProxyLib( proxyServer.pluginManager.registerListener(loader, this) } + override fun createLogger(name: String): McLogger = + JavaLogger(name) + .apply { parent = loader.logger.parent } + override fun getPlayerById(playerId: UUID): McProxyPlayer? = proxyServer.getPlayer(playerId) ?.let { getPlayerByInstance(it) } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 5d0d51a..6f53b3c 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -2,4 +2,6 @@ dependencies { api(project(":api:api-common")) compileOnly(libs.adventure.api) compileOnly(libs.adventure.gson) + + compileOnly(rootProject.libs.slf4j) } diff --git a/common/src/main/kotlin/su/plo/slib/logging/JavaLogger.kt b/common/src/main/kotlin/su/plo/slib/logging/JavaLogger.kt new file mode 100644 index 0000000..8c6e504 --- /dev/null +++ b/common/src/main/kotlin/su/plo/slib/logging/JavaLogger.kt @@ -0,0 +1,30 @@ +package su.plo.slib.logging + +import su.plo.slib.api.logging.McLogger +import java.util.logging.Level +import java.util.logging.Logger + +class JavaLogger( + name: String +) : Logger(name, null), McLogger { + + override fun trace(format: String, vararg arguments: Any) { + log(Level.FINEST, String.format(format, *arguments)) + } + + override fun debug(format: String, vararg arguments: Any) { + log(Level.ALL, String.format(format, *arguments)) + } + + override fun info(format: String, vararg arguments: Any) { + log(Level.INFO, String.format(format, *arguments)) + } + + override fun warn(format: String, vararg arguments: Any) { + log(Level.WARNING, String.format(format, *arguments)) + } + + override fun error(format: String, vararg arguments: Any) { + log(Level.SEVERE, String.format(format, *arguments)) + } +} diff --git a/common/src/main/kotlin/su/plo/slib/logging/Slf4jLogger.kt b/common/src/main/kotlin/su/plo/slib/logging/Slf4jLogger.kt new file mode 100644 index 0000000..08ff4b1 --- /dev/null +++ b/common/src/main/kotlin/su/plo/slib/logging/Slf4jLogger.kt @@ -0,0 +1,33 @@ +package su.plo.slib.logging + +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import su.plo.slib.api.logging.McLogger + +class Slf4jLogger(name: String) : McLogger { + + val logger: Logger = LoggerFactory.getLogger(name) + + override fun getName(): String = + logger.name + + override fun trace(format: String, vararg arguments: Any) { + logger.trace(format, *arguments) + } + + override fun debug(format: String, vararg arguments: Any) { + logger.debug(format, *arguments) + } + + override fun info(format: String, vararg arguments: Any) { + logger.info(format, *arguments) + } + + override fun warn(format: String, vararg arguments: Any) { + logger.warn(format, *arguments) + } + + override fun error(format: String, vararg arguments: Any) { + logger.error(format, *arguments) + } +} diff --git a/minestom/src/main/kotlin/su/plo/slib/minestom/MinestomServerLib.kt b/minestom/src/main/kotlin/su/plo/slib/minestom/MinestomServerLib.kt index 2976cca..58b84bf 100644 --- a/minestom/src/main/kotlin/su/plo/slib/minestom/MinestomServerLib.kt +++ b/minestom/src/main/kotlin/su/plo/slib/minestom/MinestomServerLib.kt @@ -13,6 +13,7 @@ import net.minestom.server.instance.Instance import su.plo.slib.api.entity.player.McGameProfile import su.plo.slib.api.event.player.McPlayerJoinEvent import su.plo.slib.api.event.player.McPlayerQuitEvent +import su.plo.slib.api.logging.McLogger import su.plo.slib.api.permission.PermissionManager import su.plo.slib.api.server.McServerLib import su.plo.slib.api.server.entity.McServerEntity @@ -20,6 +21,7 @@ import su.plo.slib.api.server.entity.player.McServerPlayer import su.plo.slib.api.server.world.McServerWorld import su.plo.slib.chat.AdventureComponentTextConverter import su.plo.slib.language.ServerTranslatorFactory +import su.plo.slib.logging.Slf4jLogger import su.plo.slib.minestom.channel.RegisterChannelHandler import su.plo.slib.minestom.channel.MinestomChannelManager import su.plo.slib.minestom.command.MinestomCommandManager @@ -77,6 +79,9 @@ class MinestomServerLib( permissionManager.clear() } + override fun createLogger(name: String): McLogger = + Slf4jLogger(name) + override fun executeInMainThread(runnable: Runnable) { MinecraftServer.getSchedulerManager().scheduleNextTick(runnable) } diff --git a/spigot/src/main/kotlin/su/plo/slib/spigot/SpigotServerLib.kt b/spigot/src/main/kotlin/su/plo/slib/spigot/SpigotServerLib.kt index 8636e54..f9fe4f3 100644 --- a/spigot/src/main/kotlin/su/plo/slib/spigot/SpigotServerLib.kt +++ b/spigot/src/main/kotlin/su/plo/slib/spigot/SpigotServerLib.kt @@ -14,12 +14,15 @@ import org.bukkit.plugin.java.JavaPlugin import su.plo.slib.api.entity.player.McGameProfile import su.plo.slib.api.event.player.McPlayerJoinEvent import su.plo.slib.api.event.player.McPlayerQuitEvent +import su.plo.slib.api.logging.McLogger import su.plo.slib.api.permission.PermissionManager import su.plo.slib.api.server.McServerLib import su.plo.slib.api.server.entity.McServerEntity import su.plo.slib.api.server.entity.player.McServerPlayer import su.plo.slib.api.server.world.McServerWorld import su.plo.slib.language.ServerTranslatorFactory +import su.plo.slib.logging.JavaLogger +import su.plo.slib.logging.Slf4jLogger import su.plo.slib.spigot.channel.RegisterChannelHandler import su.plo.slib.spigot.channel.SpigotChannelManager import su.plo.slib.spigot.chat.BaseComponentTextConverter @@ -32,6 +35,7 @@ import su.plo.slib.spigot.util.SchedulerUtil import su.plo.slib.spigot.world.SpigotServerWorld import java.io.File import java.util.* +import java.util.logging.Logger class SpigotServerLib( private val loader: JavaPlugin @@ -74,6 +78,14 @@ class SpigotServerLib( permissionManager.clear() } + override fun createLogger(name: String): McLogger = try { + Class.forName("org.slf4j.LoggerFactory") + Slf4jLogger(name) + } catch (e: ClassNotFoundException) { + JavaLogger(name) + .apply { parent = loader.logger.parent } + } + override fun executeInMainThread(runnable: Runnable) { SchedulerUtil.runTask(loader, runnable) } diff --git a/velocity/src/main/kotlin/su/plo/slib/velocity/VelocityProxyLib.kt b/velocity/src/main/kotlin/su/plo/slib/velocity/VelocityProxyLib.kt index 949d13f..e08bb28 100644 --- a/velocity/src/main/kotlin/su/plo/slib/velocity/VelocityProxyLib.kt +++ b/velocity/src/main/kotlin/su/plo/slib/velocity/VelocityProxyLib.kt @@ -10,6 +10,7 @@ import com.velocitypowered.api.proxy.ProxyServer import com.velocitypowered.api.proxy.server.RegisteredServer import su.plo.slib.api.event.player.McPlayerJoinEvent import su.plo.slib.api.event.player.McPlayerQuitEvent +import su.plo.slib.api.logging.McLogger import su.plo.slib.api.permission.PermissionManager import su.plo.slib.api.proxy.McProxyLib import su.plo.slib.api.proxy.event.command.McProxyCommandsRegisterEvent @@ -18,6 +19,7 @@ import su.plo.slib.api.proxy.player.McProxyPlayer import su.plo.slib.api.proxy.server.McProxyServerInfo import su.plo.slib.chat.AdventureComponentTextConverter import su.plo.slib.language.ServerTranslatorFactory +import su.plo.slib.logging.Slf4jLogger import su.plo.slib.velocity.channel.VelocityChannelManager import su.plo.slib.velocity.command.VelocityCommandManager import su.plo.slib.velocity.permission.VelocityPermissionSupplier @@ -66,6 +68,9 @@ class VelocityProxyLib( proxyServer.eventManager.register(plugin, this) } + override fun createLogger(name: String): McLogger = + Slf4jLogger(name) + override fun getPlayerById(playerId: UUID): McProxyPlayer? = playerById[playerId] ?: proxyServer.getPlayer(playerId).map { getPlayerByInstance(it) }.orElse(null) diff --git a/versions/src/main/kotlin/su/plo/slib/mod/ModServerLib.kt b/versions/src/main/kotlin/su/plo/slib/mod/ModServerLib.kt index fdeb216..3da0751 100644 --- a/versions/src/main/kotlin/su/plo/slib/mod/ModServerLib.kt +++ b/versions/src/main/kotlin/su/plo/slib/mod/ModServerLib.kt @@ -12,6 +12,7 @@ import su.plo.slib.api.server.entity.McServerEntity import su.plo.slib.api.entity.player.McGameProfile import su.plo.slib.api.entity.player.McPlayer import su.plo.slib.api.event.player.McPlayerQuitEvent +import su.plo.slib.api.logging.McLogger import su.plo.slib.api.server.entity.player.McServerPlayer import su.plo.slib.api.permission.PermissionManager import su.plo.slib.api.server.world.McServerWorld @@ -22,6 +23,7 @@ import su.plo.slib.mod.command.ModCommandManager import su.plo.slib.mod.entity.ModServerEntity import su.plo.slib.mod.entity.ModServerPlayer import su.plo.slib.mod.event.server.ServerStoppingEvent +import su.plo.slib.mod.logging.Log4jLogger import su.plo.slib.mod.permission.ModPermissionSupplier import su.plo.slib.mod.world.ModServerWorld import java.io.File @@ -61,6 +63,9 @@ object ModServerLib : McServerLib { override val configsFolder = File("config") + override fun createLogger(name: String): McLogger = + Log4jLogger(name) + override fun executeInMainThread(runnable: Runnable) { minecraftServer.execute(runnable) } diff --git a/versions/src/main/kotlin/su/plo/slib/mod/logging/Log4jLogger.kt b/versions/src/main/kotlin/su/plo/slib/mod/logging/Log4jLogger.kt new file mode 100644 index 0000000..606bed2 --- /dev/null +++ b/versions/src/main/kotlin/su/plo/slib/mod/logging/Log4jLogger.kt @@ -0,0 +1,34 @@ +package su.plo.slib.mod.logging + +import org.apache.logging.log4j.LogManager +import su.plo.slib.api.logging.McLogger + +class Log4jLogger( + name: String +) : McLogger { + + val logger = LogManager.getLogger(name) + + override fun getName(): String = + logger.name + + override fun trace(format: String, vararg arguments: Any) { + logger.trace(format, *arguments) + } + + override fun debug(format: String, vararg arguments: Any) { + logger.debug(format, *arguments) + } + + override fun info(format: String, vararg arguments: Any) { + logger.info(format, *arguments) + } + + override fun warn(format: String, vararg arguments: Any) { + logger.warn(format, *arguments) + } + + override fun error(format: String, vararg arguments: Any) { + logger.error(format, *arguments) + } +}