From 479b72840c0a17d17f1de31f35a7412b6fed4aa5 Mon Sep 17 00:00:00 2001 From: tursom Date: Thu, 1 Jul 2021 00:19:41 +0800 Subject: [PATCH] update --- ts-core/ts-log/build.gradle.kts | 1 + .../kotlin/cn/tursom/log/AsyncAppender.kt | 36 ++++++ .../kotlin/cn/tursom/log/ConsiderAppender.kt | 115 ++++++++++++++++++ .../main/kotlin/cn/tursom/log/JsonLayout.kt | 34 ++++++ .../kotlin/cn/tursom/log/LayoutEncoders.kt | 22 ++++ 5 files changed, 208 insertions(+) create mode 100644 ts-core/ts-log/src/main/kotlin/cn/tursom/log/AsyncAppender.kt create mode 100644 ts-core/ts-log/src/main/kotlin/cn/tursom/log/ConsiderAppender.kt create mode 100644 ts-core/ts-log/src/main/kotlin/cn/tursom/log/JsonLayout.kt create mode 100644 ts-core/ts-log/src/main/kotlin/cn/tursom/log/LayoutEncoders.kt diff --git a/ts-core/ts-log/build.gradle.kts b/ts-core/ts-log/build.gradle.kts index e73a343..a4a774b 100644 --- a/ts-core/ts-log/build.gradle.kts +++ b/ts-core/ts-log/build.gradle.kts @@ -5,6 +5,7 @@ plugins { dependencies { implementation(project(":ts-core")) + implementation(project(":ts-core:ts-delegation")) api(group = "org.slf4j", name = "slf4j-api", version = "1.7.29") api(group = "ch.qos.logback", name = "logback-core", version = "1.2.3") api(group = "ch.qos.logback", name = "logback-classic", version = "1.2.3") diff --git a/ts-core/ts-log/src/main/kotlin/cn/tursom/log/AsyncAppender.kt b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/AsyncAppender.kt new file mode 100644 index 0000000..e273a76 --- /dev/null +++ b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/AsyncAppender.kt @@ -0,0 +1,36 @@ +package cn.tursom.log + +import ch.qos.logback.classic.Level +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.core.Appender +import ch.qos.logback.core.AsyncAppenderBase +import ch.qos.logback.core.spi.AppenderAttachableImpl +import cn.tursom.core.delegation.ReflectionDelegatedField.Companion.superField + +class AsyncAppender : AsyncAppenderBase() { + private var includeCallerData = false + private val aai: AppenderAttachableImpl by superField("aai") + private var appenderCount: Int by superField("appenderCount") + + /** + * Events of level TRACE, DEBUG and INFO are deemed to be discardable. + * @param event + * @return true if the event is of level TRACE, DEBUG or INFO false otherwise. + */ + override fun isDiscardable(event: ILoggingEvent): Boolean { + val level = event.level + return level.toInt() <= Level.INFO_INT + } + + override fun preprocess(eventObject: ILoggingEvent) { + eventObject.prepareForDeferredProcessing() + if (includeCallerData) eventObject.callerData + } + + override fun addAppender(newAppender: Appender) { + addInfo("Attaching appender named [${newAppender.name}] to AsyncAppender.") + aai.addAppender(newAppender) + appenderCount++ + } +} + diff --git a/ts-core/ts-log/src/main/kotlin/cn/tursom/log/ConsiderAppender.kt b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/ConsiderAppender.kt new file mode 100644 index 0000000..42b85c0 --- /dev/null +++ b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/ConsiderAppender.kt @@ -0,0 +1,115 @@ +package cn.tursom.log + +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.core.Appender +import ch.qos.logback.core.Context +import ch.qos.logback.core.spi.FilterAttachable +import ch.qos.logback.core.spi.FilterAttachableImpl +import ch.qos.logback.core.status.Status + +class ConsiderAppender : Appender, + FilterAttachable by FilterAttachableImpl() { + + private val appenderList: MutableList> = ArrayList() + var consider: String? = null + private var activeValue: MutableList = ArrayList() + var matchIfNull: Boolean = false + private val notContain: MutableList = ArrayList() + private var name: String? = null + private var started: Boolean = true + private var context: Context? = null + + fun addAppender(appender: Appender) { + appenderList.add(appender) + } + + fun addActiveValue(activeValue: String) { + this.activeValue.add(activeValue) + } + + fun addNotContain(notContain: String) { + this.notContain.add(notContain) + } + + override fun start() { + started = true + when { + consider == null -> appenderList.clear() + consider in activeValue -> Unit + consider in notContain -> appenderList.clear() + matchIfNull -> Unit + else -> appenderList.clear() + } + appenderList.forEach(Appender::start) + } + + override fun stop() { + started = false + appenderList.forEach(Appender::stop) + } + + override fun isStarted(): Boolean = started + + override fun setContext(context: Context?) { + this.context = context + appenderList.forEach { + it.context = context + } + } + + override fun getContext(): Context? = context + + override fun addStatus(status: Status?) { + appenderList.forEach { + it.addStatus(status) + } + } + + override fun addInfo(msg: String?) { + appenderList.forEach { + it.addInfo(msg) + } + } + + override fun addInfo(msg: String?, ex: Throwable?) { + appenderList.forEach { + it.addInfo(msg, ex) + } + } + + override fun addWarn(msg: String?) { + appenderList.forEach { + it.addWarn(msg) + } + } + + override fun addWarn(msg: String?, ex: Throwable?) { + appenderList.forEach { + it.addWarn(msg, ex) + } + } + + override fun addError(msg: String?) { + appenderList.forEach { + it.addError(msg) + } + } + + override fun addError(msg: String?, ex: Throwable?) { + appenderList.forEach { + it.addError(msg, ex) + } + } + + override fun getName(): String? = name + + override fun doAppend(event: ILoggingEvent?) { + appenderList.forEach { + it.doAppend(event) + } + } + + override fun setName(name: String?) { + this.name = name + } +} \ No newline at end of file diff --git a/ts-core/ts-log/src/main/kotlin/cn/tursom/log/JsonLayout.kt b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/JsonLayout.kt new file mode 100644 index 0000000..b0d7f16 --- /dev/null +++ b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/JsonLayout.kt @@ -0,0 +1,34 @@ +package cn.tursom.log + +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.classic.spi.ThrowableProxyUtil +import ch.qos.logback.core.LayoutBase +import cn.tursom.core.ThreadLocalSimpleDateFormat +import cn.tursom.core.Utils.gson + +open class JsonLayout : LayoutBase() { + data class LayoutData( + val logger: String, + val date: String, + val time: Long, + val thread: String, + val level: String, + val message: String, + val stackTrace: String, + ) + + override fun doLayout(event: ILoggingEvent): String = buildString { + gson.toJson(getLogObject(event), this) + append('\n') + } + + protected open fun getLogObject(event: ILoggingEvent): Any = LayoutData( + event.loggerName, + ThreadLocalSimpleDateFormat.standard.format(event.timeStamp), + event.timeStamp, + event.threadName, + event.level.levelStr, + event.formattedMessage, + ThrowableProxyUtil.asString(event.throwableProxy) + ) +} \ No newline at end of file diff --git a/ts-core/ts-log/src/main/kotlin/cn/tursom/log/LayoutEncoders.kt b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/LayoutEncoders.kt new file mode 100644 index 0000000..75a1bc6 --- /dev/null +++ b/ts-core/ts-log/src/main/kotlin/cn/tursom/log/LayoutEncoders.kt @@ -0,0 +1,22 @@ +package cn.tursom.log + +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.core.Layout +import ch.qos.logback.core.encoder.Encoder +import ch.qos.logback.core.encoder.LayoutWrappingEncoder + +object LayoutEncoders { + class JsonLayoutEncoder : CustomLayoutEncoder(JsonLayout()) + + operator fun invoke( + layout: Layout + ): Encoder = CustomLayoutEncoder(layout) + + open class CustomLayoutEncoder( + layout: Layout, + ) : LayoutWrappingEncoder() { + init { + this.layout = layout + } + } +} \ No newline at end of file