From da36fafde0fb43d51d7c3dfafad0c5d34fa73579 Mon Sep 17 00:00:00 2001 From: dankito Date: Sun, 12 Jul 2020 00:20:56 +0200 Subject: [PATCH] Added overloads for directly passing message --- .../dankito/utils/multiplatform/log/Logger.kt | 16 ++++++++++ .../utils/multiplatform/log/LoggerBase.kt | 30 +++++++++++++++++++ .../utils/multiplatform/log/Slf4jLogger.kt | 30 +++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/Logger.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/Logger.kt index b14be556..da657e0e 100644 --- a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/Logger.kt +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/Logger.kt @@ -19,16 +19,32 @@ interface Logger { val isTraceEnabled: Boolean + fun fatal(message: String, exception: Throwable? = null, vararg arguments: Any) + fun fatal(exception: Throwable? = null, vararg arguments: Any, message: () -> String) + + fun error(message: String, exception: Throwable? = null, vararg arguments: Any) + fun error(exception: Throwable? = null, vararg arguments: Any, message: () -> String) + + fun warn(message: String, exception: Throwable? = null, vararg arguments: Any) + fun warn(exception: Throwable? = null, vararg arguments: Any, message: () -> String) + fun info(message: String, exception: Throwable? = null, vararg arguments: Any) + fun info(exception: Throwable? = null, vararg arguments: Any, message: () -> String) + + fun debug(message: String, exception: Throwable? = null, vararg arguments: Any) + fun debug(exception: Throwable? = null, vararg arguments: Any, message: () -> String) + + fun trace(message: String, exception: Throwable? = null, vararg arguments: Any) + fun trace(exception: Throwable? = null, vararg arguments: Any, message: () -> String) } \ No newline at end of file diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/LoggerBase.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/LoggerBase.kt index 1b197a6c..614aaa35 100644 --- a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/LoggerBase.kt +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/log/LoggerBase.kt @@ -24,30 +24,60 @@ abstract class LoggerBase( open fun isEnabled(level: LogLevel) = level.priority <= this.level.priority + override fun fatal(message: String, exception: Throwable?, vararg arguments: Any) { + logIfEnabled(LogLevel.Fatal, exception, { message }, *arguments) + } + override fun fatal(exception: Throwable?, vararg arguments: Any, message: () -> String) { logIfEnabled(LogLevel.Fatal, exception, message, *arguments) } + + override fun error(message: String, exception: Throwable?, vararg arguments: Any) { + logIfEnabled(LogLevel.Error, exception, { message }, *arguments) + } + override fun error(exception: Throwable?, vararg arguments: Any, message: () -> String) { logIfEnabled(LogLevel.Error, exception, message, *arguments) } + + override fun warn(message: String, exception: Throwable?, vararg arguments: Any) { + logIfEnabled(LogLevel.Warn, exception, { message }, *arguments) + } + override fun warn(exception: Throwable?, vararg arguments: Any, message: () -> String) { logIfEnabled(LogLevel.Warn, exception, message, *arguments) } + + override fun info(message: String, exception: Throwable?, vararg arguments: Any) { + logIfEnabled(LogLevel.Info, exception, { message }, *arguments) + } + override fun info(exception: Throwable?, vararg arguments: Any, message: () -> String) { logIfEnabled(LogLevel.Info, exception, message, *arguments) } + + override fun debug(message: String, exception: Throwable?, vararg arguments: Any) { + logIfEnabled(LogLevel.Debug, exception, { message }, *arguments) + } + override fun debug(exception: Throwable?, vararg arguments: Any, message: () -> String) { logIfEnabled(LogLevel.Debug, exception, message, *arguments) } + + override fun trace(message: String, exception: Throwable?, vararg arguments: Any) { + logIfEnabled(LogLevel.Trace, exception, { message }, *arguments) + } + override fun trace(exception: Throwable?, vararg arguments: Any, message: () -> String) { logIfEnabled(LogLevel.Trace, exception, message, *arguments) } + open fun logIfEnabled(level: LogLevel, exception: Throwable? = null, message: () -> String, vararg arguments: Any) { if (isEnabled(level)) { log(level, createMessage(exception, message(), *arguments)) diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/log/Slf4jLogger.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/log/Slf4jLogger.kt index 975e5ce9..55e0f291 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/log/Slf4jLogger.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/log/Slf4jLogger.kt @@ -26,30 +26,60 @@ open class Slf4jLogger(protected val slf4jLogger: org.slf4j.Logger) : Logger { get() = slf4jLogger.isTraceEnabled + override fun fatal(message: String, exception: Throwable?, vararg arguments: Any) { + error(message, exception, arguments) + } + override fun fatal(exception: Throwable?, vararg arguments: Any, message: () -> String) { error(exception, *arguments, message = message) } + + override fun error(message: String, exception: Throwable?, vararg arguments: Any) { + log(exception, arguments, { message }, { msg, args -> slf4jLogger.error(msg, *args) } ) + } + override fun error(exception: Throwable?, vararg arguments: Any, message: () -> String) { log(exception, arguments, message, { msg, args -> slf4jLogger.error(msg, *args) } ) } + + override fun warn(message: String, exception: Throwable?, vararg arguments: Any) { + log(exception, arguments, { message }, { msg, args -> slf4jLogger.warn(msg, *args) } ) + } + override fun warn(exception: Throwable?, vararg arguments: Any, message: () -> String) { log(exception, arguments, message, { msg, args -> slf4jLogger.warn(msg, *args) } ) } + + override fun info(message: String, exception: Throwable?, vararg arguments: Any) { + log(exception, arguments, { message }, { msg, args -> slf4jLogger.info(msg, *args) } ) + } + override fun info(exception: Throwable?, vararg arguments: Any, message: () -> String) { log(exception, arguments, message, { msg, args -> slf4jLogger.info(msg, *args) } ) } + + override fun debug(message: String, exception: Throwable?, vararg arguments: Any) { + log(exception, arguments, { message }, { msg, args -> slf4jLogger.debug(msg, *args) } ) + } + override fun debug(exception: Throwable?, vararg arguments: Any, message: () -> String) { log(exception, arguments, message, { msg, args -> slf4jLogger.debug(msg, *args) } ) } + + override fun trace(message: String, exception: Throwable?, vararg arguments: Any) { + log(exception, arguments, { message }, { msg, args -> slf4jLogger.trace(msg, *args) } ) + } + override fun trace(exception: Throwable?, vararg arguments: Any, message: () -> String) { log(exception, arguments, message, { msg, args -> slf4jLogger.trace(msg, *args) } ) } + protected open fun log(exception: Throwable?, arguments: Array, messageCreator: () -> String, logOnLevel: (String, Array) -> Unit) {