This commit is contained in:
tursom 2021-07-01 00:19:41 +08:00
parent eecc290d7a
commit 479b72840c
5 changed files with 208 additions and 0 deletions

View File

@ -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")

View File

@ -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<ILoggingEvent>() {
private var includeCallerData = false
private val aai: AppenderAttachableImpl<ILoggingEvent> 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<ILoggingEvent>) {
addInfo("Attaching appender named [${newAppender.name}] to AsyncAppender.")
aai.addAppender(newAppender)
appenderCount++
}
}

View File

@ -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<ILoggingEvent>,
FilterAttachable<ILoggingEvent> by FilterAttachableImpl() {
private val appenderList: MutableList<Appender<ILoggingEvent>> = ArrayList()
var consider: String? = null
private var activeValue: MutableList<String> = ArrayList()
var matchIfNull: Boolean = false
private val notContain: MutableList<String> = ArrayList()
private var name: String? = null
private var started: Boolean = true
private var context: Context? = null
fun addAppender(appender: Appender<ILoggingEvent>) {
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<ILoggingEvent>::start)
}
override fun stop() {
started = false
appenderList.forEach(Appender<ILoggingEvent>::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
}
}

View File

@ -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<ILoggingEvent>() {
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)
)
}

View File

@ -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<ILoggingEvent>
): Encoder<ILoggingEvent> = CustomLayoutEncoder(layout)
open class CustomLayoutEncoder(
layout: Layout<ILoggingEvent>,
) : LayoutWrappingEncoder<ILoggingEvent>() {
init {
this.layout = layout
}
}
}