Skip to content

Logger

interface Logger

Deprecated

The OpenSavvy Logger library is deprecated and won't be maintained anymore. We do not plan on deleting the library, but we do not plan on improving it either. We encourage you to switch to another logging library.

Multiplatform interface for logger implementations.

Logging for a class can be implemented as follows:

class Foo {
    init {
        log.trace { "Foo's constructor was called!" }
    }

    companion object {
        private val log = loggerFor(this)
    }
}

The messages included or excluded can be selected using level. For example, to only display error messages for a specific class, use:

class Foo {
    companion object {
        private val log = loggerFor(this)

        init {
            log.level = LogLevel.ERROR
        }
    }
}

To set the log level for the entire program, see LogLevel.default.

The available logging methods are:

They all accept the same arguments:

  • zero or more objects, which will be appended to the message

  • a lambda producing a message, which will only be evaluated if the given level is enabled.

Examples:

log.level { "Simple form" }
log.level(obj1, obj2) { "With two objects" }

Because the various logging methods are inline and check that their level is enabled early, logging using this interface is performant no matter the implementation.

The loggerFor method is used to instantiate a platform-specific implementation. On Kotlin/JS, it uses the console object. On Kotlin/JVM, it uses the Slf4j library, you will need to include a Slf4j binding in your project.

Types

Companion

object Companion

Properties

level

abstract var level: LogLevel

Deprecated

The OpenSavvy Logger library is deprecated and won't be maintained anymore. We do not plan on deleting the library, but we do not plan on improving it either. We encourage you to switch to another logging library.

The log level of this logger.

Implementations of the Logger interface may add their own system to decide whether a message is printed, additionally to this attribute. For example, LogBack has its own log level configuration, Slf4j-simple never prints trace and debug messages.

Functions

debug

inline fun Logger.debug(vararg objects: Any?, message: () -> String)

error

inline fun Logger.error(vararg objects: Any?, message: () -> String)

forceDebug

abstract fun forceDebug(message: String, vararg objects: Any?)

forceError

abstract fun forceError(message: String, vararg objects: Any?)

forceInfo

abstract fun forceInfo(message: String, vararg objects: Any?)

forceTrace

abstract fun forceTrace(message: String, vararg objects: Any?)

forceWarn

abstract fun forceWarn(message: String, vararg objects: Any?)

info

inline fun Logger.info(vararg objects: Any?, message: () -> String)

trace

inline fun Logger.trace(vararg objects: Any?, message: () -> String)

warn

inline fun Logger.warn(vararg objects: Any?, message: () -> String)