All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.kr.jadekim.logger.JLog.kt Maven / Gradle / Ivy

There is a newer version: 2.0.10
Show newest version
package kr.jadekim.logger

import co.touchlab.stately.collections.SharedHashMap
import kr.jadekim.logger.option.JLoggerOptionProvider
import kr.jadekim.logger.pipeline.*

internal expect fun init()

@Suppress("VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL")
object JLog {

    var loggerLevel = LogLevel.INFO
    var pipeline: MutableList = mutableListOf(
        LoggerNameShorter(),
        TextFormatter(),
        StdOutPrinter(),
    )

    var optionProvider = JLoggerOptionProvider.builder().build()

    private val loggers = SharedHashMap()

    init {
        init()
    }

    fun get(name: String): JLogger = loggers.getOrPut(name) {
        val option = optionProvider[name]

        JLogger(name, option.level, option.pipeline)
    }

    fun installPipe(vararg pipe: JLogPipe) {
        pipe.forEach { it.install(pipeline, pipeline.size) }
    }

    fun  installPipeBefore(reference: JLogPipe.Key, pipe: Pipe) {
        var index = pipeline.indexOfFirst { it == reference }

        if (index == -1) {
            index = 0
        }

        pipe.install(pipeline, index)
    }

    fun  installPipeAfter(reference: JLogPipe.Key, pipe: Pipe) {
        var index = pipeline.indexOfFirst { it.key == reference }

        if (index == -1) {
            index = pipeline.size
        }

        pipe.install(pipeline, index)
    }

    fun  uninstallPipe(pipeKey: JLogPipe.Key) {
        pipeline.removeAll { it.key == pipeKey }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy