Logger

class Logger(val name: String, logger: System.Logger = System.getLogger(name))

Logger class with Kotlin usability improvements. It is backed by a System.Logger instance.

Parameters

name

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

Samples

import com.hexagonkt.core.text.Ansi.RESET
import com.hexagonkt.core.text.AnsiColor.BRIGHT_WHITE
import com.hexagonkt.core.text.AnsiColor.RED_BG
import com.hexagonkt.core.text.AnsiEffect.UNDERLINE
import com.hexagonkt.core.urlOf
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.condition.DisabledInNativeImage
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import java.lang.System.Logger.Level.ERROR
import java.lang.System.Logger.Level.TRACE
import java.util.logging.LogManager
import kotlin.IllegalStateException
import kotlin.reflect.KClass
import kotlin.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.core.logging.logger'. It is a default logger created with a custom name
    (same as `Logger(LoggingManager.defaultLoggerName)`).
    """
}

classLogger.trace { "Message only evaluated if trace enabled" }
classLogger.debug { "Message only evaluated if debug enabled" }
classLogger.warn { "Message only evaluated if warn enabled" }
classLogger.info { "Message only evaluated if info enabled" }

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" }
// logger 
   //sampleEnd
}

Constructors

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

Logger class with Kotlin improvements like lazy evaluation.

constructor(name: String, logger: System.Logger = System.getLogger(name))

Properties

Link copied to clipboard
val 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 info(message: () -> Any?)

Log a message using INFO level.

Link copied to clipboard
fun isLoggable(level: System.Logger.Level): Boolean

Check if this logger is enabled for a given log level.

Link copied to clipboard
fun log(level: System.Logger.Level, message: () -> Any?)

Log a message.

fun <E : Throwable> log(level: System.Logger.Level, exception: E, message: (E) -> Any?)

Log a message, with associated exception information.

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 WARNING level.

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

Log a message using WARNING level with associated exception information.