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

cn.imkarl.http.server.log.HttpLogger.kt Maven / Gradle / Ivy

package cn.imkarl.http.server.log

import cn.imkarl.core.common.log.LogLevel
import cn.imkarl.core.common.log.LogUtils

object HttpLogger {

    internal val TAG = "HttpServer"

    internal var loggerFactory: ((name: String) -> IHttpLogger)? = null

    init {
        setLogger(NOPHttpLogger)
    }

    fun setLogger(logger: (logLevel: LogLevel, msg: String?, throwable: Throwable?) -> Unit) {
        setLogger(object : SimpleHttpLogger() {
            override fun log(logLevel: LogLevel, msg: String?, throwable: Throwable?) {
                logger.invoke(logLevel, msg, throwable)
            }
        })
    }

    fun setLogger(logger: IHttpLogger) {
        setLoggerFactory { logger }
    }

    fun setLoggerFactory(factory: (name: String) -> IHttpLogger) {
        loggerFactory = factory
    }

    fun println(logLevel: LogLevel, msg: String, throwable: Throwable? = null) {
        loggerFactory?.invoke(TAG)?.apply {
            when (logLevel) {
                LogLevel.VERBOSE -> debug(msg, throwable)
                LogLevel.INFO -> info(msg, throwable)
                LogLevel.DEBUG -> debug(msg, throwable)
                LogLevel.WARN -> warn(msg, throwable)
                LogLevel.ERROR -> error(msg, throwable)
            }
        }
    }

}

interface IHttpLogger {
    open val name: String
        get() = this::class.java.simpleName
    fun trace(msg: String?, throwable: Throwable?)
    fun debug(msg: String?, throwable: Throwable?)
    fun info(msg: String?, throwable: Throwable?)
    fun warn(msg: String?, throwable: Throwable?)
    fun error(msg: String?, throwable: Throwable?)
}

open class SimpleHttpLogger(
    private val trace: ((msg: String?, throwable: Throwable?) -> Unit)? = null,
    private val debug: ((msg: String?, throwable: Throwable?) -> Unit)? = null,
    private val info: ((msg: String?, throwable: Throwable?) -> Unit)? = null,
    private val warn: ((msg: String?, throwable: Throwable?) -> Unit)? = null,
    private val error: ((msg: String?, throwable: Throwable?) -> Unit)? = null,
) : IHttpLogger {
    final override fun trace(msg: String?, throwable: Throwable?) {
        trace?.invoke(msg, throwable)
        log(LogLevel.DEBUG, msg, throwable)
    }

    final override fun debug(msg: String?, throwable: Throwable?) {
        debug?.invoke(msg, throwable)
        log(LogLevel.DEBUG, msg, throwable)
    }

    final override fun info(msg: String?, throwable: Throwable?) {
        info?.invoke(msg, throwable)
        log(LogLevel.INFO, msg, throwable)
    }

    final override fun warn(msg: String?, throwable: Throwable?) {
        warn?.invoke(msg, throwable)
        log(LogLevel.WARN, msg, throwable)
    }

    final override fun error(msg: String?, throwable: Throwable?) {
        error?.invoke(msg, throwable)
        log(LogLevel.ERROR, msg, throwable,)
    }
    open fun log(logLevel: LogLevel, msg: String?, throwable: Throwable?) {
        if (!msg.isNullOrBlank()) {
            LogUtils.println(logLevel, name, msg)
        }
        if (throwable != null) {
            LogUtils.println(logLevel, name, throwable.stackTraceToString())
        }
    }
}

object NOPHttpLogger: IHttpLogger {
    override fun trace(msg: String?, throwable: Throwable?) {
    }

    override fun debug(msg: String?, throwable: Throwable?) {
    }

    override fun info(msg: String?, throwable: Throwable?) {
    }

    override fun warn(msg: String?, throwable: Throwable?) {
    }

    override fun error(msg: String?, throwable: Throwable?) {
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy