From 4cfbb1e1e79196b402a3482ff63258059a784d42 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Thu, 8 Feb 2024 11:27:29 +0100 Subject: [PATCH] Wrap in statement to only log once, change level to warn --- .../arbjerg/lavalink/internal/LavalinkSocket.kt | 6 ++---- .../dev/arbjerg/lavalink/internal/logging.kt | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/dev/arbjerg/lavalink/internal/logging.kt diff --git a/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt b/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt index d394b5b..acbfad5 100644 --- a/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt +++ b/src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt @@ -142,8 +142,7 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos } is ConnectException -> { - logger.error("Failed to connect to WS of ${node.name} (${node.baseUri}), retrying in ${reconnectInterval / 1000} seconds") - logger.trace("Failed to connect to WS of ${node.name} (${node.baseUri}), retrying in ${reconnectInterval / 1000} seconds", t) + logger.warnOrTrace("Failed to connect to WS of ${node.name} (${node.baseUri}), retrying in ${reconnectInterval / 1000} seconds", t) } is SocketException -> { @@ -153,8 +152,7 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos return } - logger.error("Socket error on ${node.name}, reconnecting in ${reconnectInterval / 1000} seconds") - logger.trace("Socket error on ${node.name}, reconnecting in ${reconnectInterval / 1000} seconds", t) + logger.warnOrTrace("Socket error on ${node.name}, reconnecting in ${reconnectInterval / 1000} seconds", t) node.available = false open = false } diff --git a/src/main/kotlin/dev/arbjerg/lavalink/internal/logging.kt b/src/main/kotlin/dev/arbjerg/lavalink/internal/logging.kt new file mode 100644 index 0000000..0f4b3be --- /dev/null +++ b/src/main/kotlin/dev/arbjerg/lavalink/internal/logging.kt @@ -0,0 +1,15 @@ +package dev.arbjerg.lavalink.internal + +import org.slf4j.Logger + +/** + * Log a trace message to the console. + * If [Logger.isTraceEnabled] returns false, it will log [message] as warning instead. + */ +fun Logger.warnOrTrace(message: String, t: Throwable) { + if (this.isTraceEnabled) { + this.trace(message, t) + } else { + this.warn(message) + } +}