Skip to content

Commit

Permalink
feat: loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Mar 13, 2024
1 parent fd79574 commit cc70ca6
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 8 deletions.
3 changes: 0 additions & 3 deletions api/common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
dependencies {

}
8 changes: 8 additions & 0 deletions api/common/src/main/kotlin/su/plo/slib/api/McLib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
52 changes: 52 additions & 0 deletions api/common/src/main/kotlin/su/plo/slib/api/logging/McLogger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ subprojects {
implementation(rootProject.libs.kotlinx.coroutines)
implementation(rootProject.libs.kotlinx.coroutines.jdk8)

implementation(rootProject.libs.slf4j)
implementation(rootProject.libs.guava)
}

Expand Down
9 changes: 7 additions & 2 deletions bungee/src/main/kotlin/su/plo/slib/bungee/BungeeProxyLib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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) }
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ dependencies {
api(project(":api:api-common"))
compileOnly(libs.adventure.api)
compileOnly(libs.adventure.gson)

compileOnly(rootProject.libs.slf4j)
}
30 changes: 30 additions & 0 deletions common/src/main/kotlin/su/plo/slib/logging/JavaLogger.kt
Original file line number Diff line number Diff line change
@@ -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))
}
}
33 changes: 33 additions & 0 deletions common/src/main/kotlin/su/plo/slib/logging/Slf4jLogger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ 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
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
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions spigot/src/main/kotlin/su/plo/slib/spigot/SpigotServerLib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions versions/src/main/kotlin/su/plo/slib/mod/ModServerLib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
34 changes: 34 additions & 0 deletions versions/src/main/kotlin/su/plo/slib/mod/logging/Log4jLogger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit cc70ca6

Please sign in to comment.