Skip to content

Commit

Permalink
Replace logger
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed May 9, 2024
1 parent e8eeb2e commit d415b82
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 144 deletions.
1 change: 1 addition & 0 deletions pcbridge-spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation("net.kyori:adventure-text-minimessage:4.16.0")
compileOnly("org.spongepowered:configurate-gson:4.0.0")
implementation("org.spongepowered:configurate-extra-kotlin:4.1.2")
implementation("io.github.oshai:kotlin-logging-jvm:5.1.0")

// Testing
// testImplementation("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT")
Expand Down
29 changes: 4 additions & 25 deletions pcbridge-spigot/src/main/kotlin/com/projectcitybuild/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ import com.projectcitybuild.features.warnings.actions.GetUnacknowledgedWarnings
import com.projectcitybuild.features.warnings.commands.WarningAcknowledgeCommand
import com.projectcitybuild.features.warnings.listeners.NotifyWarningsOnJoinListener
import com.projectcitybuild.features.warnings.repositories.PlayerWarningRepository
import com.projectcitybuild.support.PlatformLogger
import com.projectcitybuild.support.spigot.SpigotCommandRegistry
import com.projectcitybuild.support.spigot.SpigotListenerRegistry
import com.projectcitybuild.support.spigot.SpigotLogger
import com.projectcitybuild.support.spigot.SpigotNamespace
import com.projectcitybuild.support.spigot.SpigotTimer
import io.github.reactivecircus.cache4k.Cache
Expand Down Expand Up @@ -123,10 +121,6 @@ private fun Module.spigot(plugin: JavaPlugin) {
get<JavaPlugin>().server
}

single<PlatformLogger> {
SpigotLogger(get<JavaPlugin>().logger)
}

single {
BukkitAudiences.create(get<JavaPlugin>())
}
Expand All @@ -151,9 +145,7 @@ private fun Module.spigot(plugin: JavaPlugin) {
}

single<Permissions> {
LuckPermsPermissions(
logger = get(),
)
LuckPermsPermissions()
}

factory {
Expand All @@ -174,7 +166,7 @@ private fun Module.core() {
}

single {
DatabaseSession(logger = get()).apply {
DatabaseSession().apply {
val configProvider = get<Config>()
val config = configProvider.load()
connect(DatabaseSource.fromConfig(config))
Expand All @@ -187,7 +179,6 @@ private fun Module.core() {
single {
SentryReporter(
config = get(),
logger = get(),
).apply {
val configProvider = get<Config>()
val config = configProvider.load()
Expand Down Expand Up @@ -221,11 +212,7 @@ private fun Module.core() {
)
}

single {
Store(
logger = get(),
)
}
single { Store() }
}

private fun Module.http() {
Expand All @@ -245,7 +232,6 @@ private fun Module.integrations() {
DynmapIntegration(
plugin = get(),
config = get(),
logger = get(),
sentry = get(),
warpRepository = get(),
)
Expand All @@ -254,15 +240,12 @@ private fun Module.integrations() {
single {
EssentialsIntegration(
plugin = get(),
logger = get(),
sentry = get(),
)
}

single {
LuckPermsIntegration(
logger = get(),
)
LuckPermsIntegration()
}
}

Expand All @@ -280,7 +263,6 @@ private fun Module.announcements() {
config = get(),
timer = get(),
server = get(),
logger = get(),
plugin = get(),
)
}
Expand Down Expand Up @@ -332,7 +314,6 @@ private fun Module.joinMessages() {
factory {
FirstTimeJoinListener(
config = get(),
logger = get(),
server = get(),
playerConfigRepository = get(),
time = get(),
Expand Down Expand Up @@ -425,7 +406,6 @@ private fun Module.bans() {
aggregateRepository = get(),
authoriseConnection = AuthoriseConnection(),
dateTimeFormatter = get(),
logger = get(),
sentry = get(),
server = get(),
minecraftDispatcher = { get<JavaPlugin>().minecraftDispatcher },
Expand Down Expand Up @@ -578,7 +558,6 @@ private fun Module.sync() {
playerGroupHttpService = get<HttpService>().playerGroup,
accountLinkHttpService = get<HttpService>().verificationURL,
config = get(),
logger = get(),
)
}

Expand Down
12 changes: 3 additions & 9 deletions pcbridge-spigot/src/main/kotlin/com/projectcitybuild/PCBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.projectcitybuild

import com.github.shynixn.mccoroutine.bukkit.SuspendingJavaPlugin
import com.projectcitybuild.core.errors.SentryReporter
import com.projectcitybuild.core.errors.trace
import com.projectcitybuild.features.announcements.listeners.AnnouncementEnableListener
import com.projectcitybuild.features.bans.commands.BanCommand
import com.projectcitybuild.features.bans.commands.BanIPCommand
Expand Down Expand Up @@ -87,7 +88,7 @@ private class Lifecycle: KoinComponent {
private val commandRegistry: SpigotCommandRegistry by inject()
private val listenerRegistry: SpigotListenerRegistry by inject()

suspend fun boot() = trace {
suspend fun boot() = sentry.trace {
commandRegistry.apply {
register(
handler = get<PCBridgeCommand>(),
Expand Down Expand Up @@ -179,7 +180,7 @@ private class Lifecycle: KoinComponent {
get<LuckPermsIntegration>().enable()
}

suspend fun shutdown() = trace {
suspend fun shutdown() = sentry.trace {
get<SpigotTimer>().cancelAll()

get<DynmapIntegration>().disable()
Expand All @@ -190,13 +191,6 @@ private class Lifecycle: KoinComponent {
commandRegistry.unregisterAll()
audiences.close()
}

private suspend fun <R> trace(block: suspend () -> R): Result<R> {
return runCatching { block() }.onFailure {
sentry.report(it)
throw it
}
}
}

private val logo = """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.projectcitybuild.core.database

import com.projectcitybuild.support.PlatformLogger
import com.projectcitybuild.core.logger.logger
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import java.sql.Connection

class DatabaseSession(
private val logger: PlatformLogger,
) {
class DatabaseSession {
private var database: HikariDataSource? = null

fun connect(source: DatabaseSource) {
val url = "jdbc:mysql://${source.hostName}:${source.port}/${source.databaseName}"
logger.debug("Connecting to $url")
logger.debug {"Connecting to $url" }

val config = HikariConfig().apply {
jdbcUrl = url
Expand All @@ -36,19 +34,19 @@ class DatabaseSession(
check (hasDatabase(source.databaseName)) {
"Database not found: ${source.databaseName}"
}
logger.info("Database connection established")
logger.info { "Database connection established" }
}

fun disconnect() {
database?.close()
database = null

logger.info("Database connection closed")
logger.info { "Database connection closed" }
}

fun <T> connect(action: (Connection) -> T): T {
if (database == null) {
logger.warning("Tried to access database before connecting")
logger.warn { "Tried to access database before connecting" }
}
val connection = database?.connection
checkNotNull(connection)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.projectcitybuild.core.errors

import com.projectcitybuild.core.config.Config
import com.projectcitybuild.support.PlatformLogger
import com.projectcitybuild.core.logger.logger
import io.sentry.Sentry

class SentryReporter(
private val config: Config,
private val logger: PlatformLogger,
) {
private var started = false

Expand All @@ -15,13 +14,13 @@ class SentryReporter(
options.dsn = config.load().errorReporting.sentryDsn
}
started = true
logger.info("Sentry error reporting enabled")
logger.info { "Sentry error reporting enabled" }
}

fun close() {
if (started) {
Sentry.close()
logger.info("Sentry error reporting disabled")
logger.info { "Sentry error reporting disabled" }
}
}

Expand All @@ -31,3 +30,10 @@ class SentryReporter(
}
}
}

suspend fun <R> SentryReporter.trace(block: suspend () -> R): Result<R> {
return runCatching { block() }.onFailure {
report(it)
throw it
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.projectcitybuild.core.logger

import io.github.oshai.kotlinlogging.KotlinLogging

val logger = KotlinLogging.logger("pcbridge")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.projectcitybuild.core.permissions.adapters

import com.projectcitybuild.core.logger.logger
import com.projectcitybuild.core.permissions.Permissions
import com.projectcitybuild.support.PlatformLogger
import net.luckperms.api.LuckPerms
import net.luckperms.api.LuckPermsProvider
import net.luckperms.api.model.user.User
Expand All @@ -10,9 +10,7 @@ import net.luckperms.api.node.types.InheritanceNode
import java.util.UUID
import java.util.stream.Collectors

class LuckPermsPermissions(
private val logger: PlatformLogger,
) : Permissions {
class LuckPermsPermissions: Permissions {
class PermissionUserNotFoundException() : Exception()

private val luckPerms: LuckPerms
Expand All @@ -21,7 +19,7 @@ class LuckPermsPermissions(
private fun getUser(playerUUID: UUID): User {
val user = luckPerms.userManager.getUser(playerUUID)
if (user == null) {
logger.severe("Could not load user ($playerUUID) from permissions manager")
logger.error { "Could not load user ($playerUUID) from permissions manager" }
throw PermissionUserNotFoundException()
}
return user
Expand All @@ -41,8 +39,7 @@ class LuckPermsPermissions(
groupNames.forEach { groupName ->
val groupNode = InheritanceNode.builder(groupName).build()
user.data().add(groupNode)

logger.verbose("Assigning to $groupName group")
logger.debug { "Assigning to $groupName group" }
}

luckPerms.userManager.saveUser(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.projectcitybuild.core.state

import com.projectcitybuild.support.PlatformLogger
import com.projectcitybuild.core.logger.logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext

private val mutex = Mutex()

class Store(
private val logger: PlatformLogger,
) {
class Store {
val state: ServerState
get() = _state

Expand All @@ -20,11 +18,11 @@ class Store(
)

suspend fun mutate(mutation: (ServerState) -> ServerState) = withContext(Dispatchers.Default) {
logger.debug("[previous state]\n$state")
logger.debug { "[previous state]\n$state" }

mutex.withLock {
_state = mutation(_state)
logger.debug("[new state]\n$state")
logger.debug { "[new state]\n$state" }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.projectcitybuild.features.announcements.listeners

import com.projectcitybuild.core.config.Config
import com.projectcitybuild.core.logger.logger
import com.projectcitybuild.features.announcements.actions.StartAnnouncementTimer
import com.projectcitybuild.features.announcements.repositories.AnnouncementRepository
import com.projectcitybuild.support.PlatformLogger
import com.projectcitybuild.support.spigot.SpigotTimer
import org.bukkit.Server
import org.bukkit.event.EventHandler
Expand All @@ -18,7 +18,6 @@ class AnnouncementEnableListener(
private val config: Config,
private val timer: SpigotTimer,
private val server: Server,
private val logger: PlatformLogger,
private val plugin: JavaPlugin,
): Listener {
private var action: StartAnnouncementTimer? = null
Expand All @@ -37,7 +36,7 @@ class AnnouncementEnableListener(
)
action?.start()

logger.debug("Announcement timer started")
logger.debug { "Announcement timer started" }
}

@EventHandler
Expand All @@ -49,6 +48,6 @@ class AnnouncementEnableListener(
action?.stop()
action = null

logger.debug("Announcement timer stopped")
logger.debug { "Announcement timer stopped" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.projectcitybuild.core.state.Store
import com.projectcitybuild.features.bans.actions.AuthoriseConnection
import com.projectcitybuild.features.bans.events.ConnectionPermittedEvent
import com.projectcitybuild.core.datetime.formatter.DateTimeFormatter
import com.projectcitybuild.core.logger.logger
import com.projectcitybuild.pcbridge.http.responses.Aggregate
import com.projectcitybuild.features.bans.repositories.AggregateRepository
import com.projectcitybuild.features.bans.Sanitizer
import com.projectcitybuild.support.PlatformLogger
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import net.kyori.adventure.text.Component
Expand All @@ -25,7 +25,6 @@ import kotlin.coroutines.CoroutineContext
class AuthorizeConnectionListener(
private val aggregateRepository: AggregateRepository,
private val authoriseConnection: AuthoriseConnection,
private val logger: PlatformLogger,
private val dateTimeFormatter: DateTimeFormatter,
private val sentry: SentryReporter,
private val server: Server,
Expand Down Expand Up @@ -76,7 +75,7 @@ class AuthorizeConnectionListener(
)
}
}.onFailure {
logger.severe(it.localizedMessage)
logger.error { it.localizedMessage }
sentry.report(it)

// If something goes wrong, better not to let players in
Expand Down
Loading

0 comments on commit d415b82

Please sign in to comment.