Added overloads for directly passing message

This commit is contained in:
dankito 2020-07-12 00:20:56 +02:00
parent 4ef5243bbe
commit da36fafde0
3 changed files with 76 additions and 0 deletions

View File

@ -19,16 +19,32 @@ interface Logger {
val isTraceEnabled: Boolean val isTraceEnabled: Boolean
fun fatal(message: String, exception: Throwable? = null, vararg arguments: Any)
fun fatal(exception: Throwable? = null, vararg arguments: Any, message: () -> String) 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 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 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 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 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) fun trace(exception: Throwable? = null, vararg arguments: Any, message: () -> String)
} }

View File

@ -24,30 +24,60 @@ abstract class LoggerBase(
open fun isEnabled(level: LogLevel) = level.priority <= this.level.priority 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) { override fun fatal(exception: Throwable?, vararg arguments: Any, message: () -> String) {
logIfEnabled(LogLevel.Fatal, exception, message, *arguments) 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) { override fun error(exception: Throwable?, vararg arguments: Any, message: () -> String) {
logIfEnabled(LogLevel.Error, exception, message, *arguments) 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) { override fun warn(exception: Throwable?, vararg arguments: Any, message: () -> String) {
logIfEnabled(LogLevel.Warn, exception, message, *arguments) 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) { override fun info(exception: Throwable?, vararg arguments: Any, message: () -> String) {
logIfEnabled(LogLevel.Info, exception, message, *arguments) 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) { override fun debug(exception: Throwable?, vararg arguments: Any, message: () -> String) {
logIfEnabled(LogLevel.Debug, exception, message, *arguments) 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) { override fun trace(exception: Throwable?, vararg arguments: Any, message: () -> String) {
logIfEnabled(LogLevel.Trace, exception, message, *arguments) logIfEnabled(LogLevel.Trace, exception, message, *arguments)
} }
open fun logIfEnabled(level: LogLevel, exception: Throwable? = null, message: () -> String, vararg arguments: Any) { open fun logIfEnabled(level: LogLevel, exception: Throwable? = null, message: () -> String, vararg arguments: Any) {
if (isEnabled(level)) { if (isEnabled(level)) {
log(level, createMessage(exception, message(), *arguments)) log(level, createMessage(exception, message(), *arguments))

View File

@ -26,30 +26,60 @@ open class Slf4jLogger(protected val slf4jLogger: org.slf4j.Logger) : Logger {
get() = slf4jLogger.isTraceEnabled 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) { override fun fatal(exception: Throwable?, vararg arguments: Any, message: () -> String) {
error(exception, *arguments, message = message) 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) { override fun error(exception: Throwable?, vararg arguments: Any, message: () -> String) {
log(exception, arguments, message, { msg, args -> slf4jLogger.error(msg, *args) } ) 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) { override fun warn(exception: Throwable?, vararg arguments: Any, message: () -> String) {
log(exception, arguments, message, { msg, args -> slf4jLogger.warn(msg, *args) } ) 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) { override fun info(exception: Throwable?, vararg arguments: Any, message: () -> String) {
log(exception, arguments, message, { msg, args -> slf4jLogger.info(msg, *args) } ) 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) { override fun debug(exception: Throwable?, vararg arguments: Any, message: () -> String) {
log(exception, arguments, message, { msg, args -> slf4jLogger.debug(msg, *args) } ) 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) { override fun trace(exception: Throwable?, vararg arguments: Any, message: () -> String) {
log(exception, arguments, message, { msg, args -> slf4jLogger.trace(msg, *args) } ) log(exception, arguments, message, { msg, args -> slf4jLogger.trace(msg, *args) } )
} }
protected open fun log(exception: Throwable?, arguments: Array<out Any>, messageCreator: () -> String, protected open fun log(exception: Throwable?, arguments: Array<out Any>, messageCreator: () -> String,
logOnLevel: (String, Array<out Any>) -> Unit) { logOnLevel: (String, Array<out Any>) -> Unit) {