Home

com.hexagonkt.helpers / Logger

Logger

class Logger

Logger class with Kotlin improvements like lazy evaluation. It is backed by a SLF4J compatible logger.

Usage example:

// logger
val classLogger: Logger = Logger(Runtime::class) // Logger for the `Runtime` class
val instanceLogger: Logger = Logger(this) // Logger for this instance's class

logger.info {
    """
    You can add a quick log without declaring a Logger using 'com.hexagonkt.helpers.logger'.
    It is a default logger created for the System class (same as `Logger(System::class)`).
    """
}

classLogger.trace { "Message only evaluated if trace enabled at ${Jvm.id}" }
classLogger.debug { "Message only evaluated if debug enabled at ${Jvm.id}" }
classLogger.warn { "Message only evaluated if warn enabled at ${Jvm.id}" }
classLogger.info { "Message only evaluated if info enabled at ${Jvm.id}" }

val exception = IllegalStateException("Exception")
classLogger.warn(exception) { "Warning with exception" }
classLogger.error(exception) { "Error message with exception" }
classLogger.error { "Error without an exception" }

classLogger.time("Logs the time used to run the following block of code") {
    val message = "Block of code to be timed"
    assert(message.isNotBlank())
}

instanceLogger.flare { "Prints a log that stands out for ease searching" }
// logger

Parameters

type - Logger type. It is shown in the logs messages and used for log filtering.

Constructors

Name Summary
<init> Logger(instance:Any)``Logger(type:KClass<*>)
Logger class with Kotlin improvements like lazy evaluation. It is backed by a SLF4J compatible logger.

Functions

Name Summary
debug fun debug(message: () ->Any?):Unit
error fun error(message: () ->Any?):Unit
fun <E :Throwable> error(exception:E, message: (E) ->Any?):Unit
flare fun flare(message: () ->Any? = { "" }):Unit
info fun info(message: () ->Any?):Unit
time fun time(startNanos:Long, message: () ->Any? = { "" }):Unit
fun <T> time(message: () ->Any? = { null }, block: () ->T):T
fun <T> time(message:Any?, block: () ->T):T
trace fun trace(message: () ->Any?):Unit
warn fun warn(message: () ->Any?):Unit
fun <E :Throwable> warn(exception:E, message: (E) ->Any?):Unit

Extension Functions

Name Summary
convertToMap funAny.convertToMap():Map<*, *>
serialize funAny.serialize(format:SerializationFormat= defaultFormat):String
funAny.serialize(contentType:String):String