Logger

class Logger(val name: String)

Logger class with Kotlin improvements like lazy evaluation. It is backed by a logging port.

Samples

import com.hexagonkt.core.logging.logger
import com.hexagonkt.core.logging.Logger
import com.hexagonkt.core.logging.LoggingLevel.*
import com.hexagonkt.core.logging.LoggingManager
import org.junit.jupiter.api.Test
fun main() { 
   //sampleStart 
   // logger
val classLogger: Logger = Logger(Runtime::class) // Logger for the `Runtime` class
val instanceLogger: Logger = Logger(this::class) // Logger for this instance's class

logger.info {
    """
    You can add a quick log without declaring a Logger with '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.warn(exception)
classLogger.error(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" }

// Logging level can be changed programmatically
LoggingManager.setLoggerLevel(ERROR)
LoggingManager.setLoggerLevel(classLogger, DEBUG)
LoggingManager.setLoggerLevel("com.hexagonkt", INFO)
// logger 
   //sampleEnd
}

Parameters

name

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

Constructors

Link copied to clipboard
fun Logger(type: KClass<*>)

Logger class with Kotlin improvements like lazy evaluation.

Link copied to clipboard
fun Logger(name: String)

Functions

Link copied to clipboard
fun debug(message: () -> Any?)

Log a message using DEBUG level.

Link copied to clipboard
fun error(message: () -> Any?)

Log a message using ERROR level.

fun <E : Throwable> error(exception: E?, message: (E?) -> Any? = { "" })

Log a message using ERROR level with associated exception information.

Link copied to clipboard
fun flare(message: () -> Any? = { "" })

Log a message using TRACE level.

Link copied to clipboard
fun info(message: () -> Any?)

Log a message using INFO level.

Link copied to clipboard
fun isDebugEnabled(): Boolean

Check if the DEBUG logging level is enabled for this logger.

Link copied to clipboard
fun isErrorEnabled(): Boolean

Check if the ERROR logging level is enabled for this logger.

Link copied to clipboard
fun isInfoEnabled(): Boolean

Check if the INFO logging level is enabled for this logger.

Link copied to clipboard
fun isLoggerLevelEnabled(level: LoggingLevel): Boolean

Check if a logging level is enabled for this logger.

Link copied to clipboard
fun isTraceEnabled(): Boolean

Check if the TRACE logging level is enabled for this logger.

Link copied to clipboard
fun isWarnEnabled(): Boolean

Check if the WARN logging level is enabled for this logger.

Link copied to clipboard
fun setLoggerLevel(level: LoggingLevel)

Set a logging level for this logger.

Link copied to clipboard
fun <T> time(message: Any?, block: () -> T): T
fun <T> time(message: () -> Any? = { null }, block: () -> T): T

Execute a lambda block and log a message using TRACE level with the logging time.

fun time(startNanos: Long, message: () -> Any? = { "" })

Log a message using TRACE level with the logging time.

Link copied to clipboard
fun trace(message: () -> Any?)

Log a message using TRACE level.

Link copied to clipboard
fun warn(message: () -> Any?)

Log a message using WARN level.

fun <E : Throwable> warn(exception: E?, message: (E?) -> Any? = { "" })

Log a message using WARN level with associated exception information.

Properties

Link copied to clipboard
val name: String