io.cloudshiftdev.logging.slf4j.Slf4jLoggerAdapter.kt Maven / Gradle / Ivy
package io.cloudshiftdev.logging.slf4j
import io.cloudshiftdev.logging.api.LogLevel
import io.cloudshiftdev.logging.spi.LoggerAdapter
import io.cloudshiftdev.logging.spi.LoggingEvent
import org.slf4j.Logger
public class Slf4jLoggerAdapter(private val logger: Logger) : LoggerAdapter {
override suspend fun isEnabled(level: LogLevel): Boolean =
logger.isEnabledForLevel(level.toSlf4jLevel())
override suspend fun log(event: LoggingEvent) {
val builder = logger.atLevel(event.level.toSlf4jLevel()).setCause(event.thrown)
event.tags.forEach { (key, value) -> builder.addKeyValue(key, value) }
builder.log(event.message)
}
}
private fun LogLevel.toSlf4jLevel(): org.slf4j.event.Level {
return when (this) {
LogLevel.Trace -> org.slf4j.event.Level.TRACE
LogLevel.Debug -> org.slf4j.event.Level.DEBUG
LogLevel.Info -> org.slf4j.event.Level.INFO
LogLevel.Warn -> org.slf4j.event.Level.WARN
LogLevel.Error -> org.slf4j.event.Level.ERROR
}
}
// TODO - benchmark map lookup vs when, for each log level
// private val logLevelToSlf4jLevel =
// mapOf(
// LogLevel.Trace to org.slf4j.event.Level.TRACE,
// LogLevel.Debug to org.slf4j.event.Level.DEBUG,
// LogLevel.Info to org.slf4j.event.Level.INFO,
// LogLevel.Warn to org.slf4j.event.Level.WARN,
// LogLevel.Error to org.slf4j.event.Level.ERROR,
// )
// .let { map -> IdentityHashMap(map) }
//
// private fun LogLevel.toSlf4jLevel2(): org.slf4j.event.Level =
// logLevelToSlf4jLevel[this] ?: error("Unknown log level: $this")
© 2015 - 2025 Weber Informatics LLC | Privacy Policy