touchlab / Kermit

Kermit by Touchlab is a Kotlin Multiplatform centralized logging utility.
https://kermit.touchlab.co
Apache License 2.0
725 stars 41 forks source link

Logging on a desktop JVM #263

Closed mipastgt closed 1 year ago

mipastgt commented 2 years ago

While reading the documentation I can't find any hints on logging on a desktop JVM. Isn't that supported or did I just miss something. Actually I was hoping to find some logging interface to the popular SLF4J facade which is heavily used in the JVM world.

psh commented 1 year ago

It's true, Kermit doesnt have built-in support for SLF4J. That said, it does support users who want to supply their own implementation of the abstract LogWriter class - it boils down to implementing one method - so might be as simple as writing something along the lines of

class Slf4jLogWriter : LogWriter() {
    override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) {
        val builder = LoggerFactory.getLogger(tag)
            .atLevel(severity.slf4jLevel)

        if (throwable != null) {
            builder.setCause(throwable)
        }

        builder.log(message)
    }

    // No perfect match between the respective logging levels, but this might be close enough
    private val Severity.slf4jLevel: Level
        get() = when (this) {
            Severity.Verbose -> Level.TRACE
            Severity.Debug -> Level.DEBUG
            Severity.Info -> Level.INFO
            Severity.Warn -> Level.WARN
            Severity.Error -> Level.ERROR
            Severity.Assert -> Level.ERROR
        }
}