添加日志工具类

This commit is contained in:
tursom 2019-11-30 10:28:23 +08:00
parent f2bd0d2ae3
commit 0229dbe0df
5 changed files with 47 additions and 5 deletions

View File

@ -4,6 +4,6 @@ import org.slf4j.Logger
interface Slf4j {
val log: Logger
val logger: Logger get() = log
val sfl4j: Logger get() = log
val logger get() = log
val sfl4j get() = log
}

View File

@ -0,0 +1,9 @@
package cn.tursom.log
import org.slf4j.Logger
interface TrySlf4j {
val log: Logger?
val logger get() = log
val sfl4j get() = log
}

View File

@ -3,11 +3,17 @@ package cn.tursom.log.impl
import cn.tursom.log.Slf4j
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName
class Slf4jImpl(name: String? = null) : Slf4j {
override val log: Logger = LoggerFactory.getLogger(name ?: if (this::class.java != Slf4jImpl::class.java) {
val clazz = this::class
constructor(clazz: Class<*>?) : this(clazz?.name)
constructor(clazz: KClass<*>?) : this(clazz?.jvmName?.let {
if (clazz.isCompanion) it.dropLast(10) else it
})
override val log: Logger = LoggerFactory.getLogger(name ?: if (this.javaClass != Slf4jImpl::class.java) {
val clazz = this.javaClass.kotlin
clazz.jvmName.let {
if (clazz.isCompanion) it.dropLast(10) else it
}

View File

@ -0,0 +1,27 @@
package cn.tursom.log.impl
import cn.tursom.log.TrySlf4j
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName
class TrySlf4jImpl(name: String? = null) : TrySlf4j {
constructor(clazz: Class<*>?) : this(clazz?.name)
constructor(clazz: KClass<*>?) : this(clazz?.jvmName?.let {
if (clazz.isCompanion) it.dropLast(10) else it
})
override val log: Logger? = try {
LoggerFactory.getLogger(name ?: if (this.javaClass != TrySlf4jImpl::class.java) {
val clazz = this.javaClass.kotlin
clazz.jvmName.let {
if (clazz.isCompanion) it.dropLast(10) else it
}
} else {
throw NotImplementedError("")
})
} catch (e: Throwable) {
null
}
}

View File

@ -1,4 +1,4 @@
dependencies {
implementation project(":")
compileOnly project(":log")
compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
}