From 39775c2fa0787e5ff96b5dcd983e12706436001d Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Mon, 17 Aug 2020 22:21:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=B3=BB=E7=BB=9F=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E9=87=8D=E5=AE=9A=E5=90=91=E4=B8=BAlog4j=E7=AD=89jvm?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=B3=BB=E7=BB=9F=20(#498)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Close #395 * LoggerAdapters --- buildSrc/src/main/kotlin/Versions.kt | 4 ++ mirai-core/build.gradle.kts | 2 + .../net/mamoe/mirai/utils/LoggerAdapters.kt | 32 +++++++++++++++ .../mirai/utils/internal/logging/JdkLogger.kt | 40 +++++++++++++++++++ .../utils/internal/logging/Log4jLogger.kt | 40 +++++++++++++++++++ .../utils/internal/logging/Slf4jLogger.kt | 38 ++++++++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/LoggerAdapters.kt create mode 100644 mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/JdkLogger.kt create mode 100644 mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Log4jLogger.kt create mode 100644 mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Slf4jLogger.kt diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 04fc1a799..c3736c931 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -36,6 +36,10 @@ object Versions { const val bintray = "1.8.5" } + object Logging { + const val slf4j = "1.7.30" + const val log4j = "2.13.3" + } } @Suppress("unused") diff --git a/mirai-core/build.gradle.kts b/mirai-core/build.gradle.kts index a25c55564..0be86a1a5 100644 --- a/mirai-core/build.gradle.kts +++ b/mirai-core/build.gradle.kts @@ -108,6 +108,8 @@ kotlin { //api(kotlin("stdlib-jdk8")) //api(kotlin("stdlib-jdk7")) api(kotlin("reflect")) + compileOnly("org.apache.logging.log4j:log4j-api:" + Versions.Logging.log4j) + compileOnly("org.slf4j:slf4j-api:" + Versions.Logging.slf4j) api(ktor("client-core-jvm", Versions.Kotlin.ktor)) implementation(kotlinx("io-jvm", Versions.Kotlin.io)) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/LoggerAdapters.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/LoggerAdapters.kt new file mode 100644 index 000000000..d0ce5804d --- /dev/null +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/LoggerAdapters.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.utils + +import net.mamoe.mirai.utils.internal.logging.JdkLogger +import net.mamoe.mirai.utils.internal.logging.Log4jLogger +import net.mamoe.mirai.utils.internal.logging.Slf4jLogger + +@SinceMirai("1.2.0") +public object LoggerAdapters { + @JvmStatic + public fun java.util.logging.Logger.asMiraiLogger(): MiraiLogger { + return JdkLogger(this) + } + + @JvmStatic + public fun org.apache.logging.log4j.Logger.asMiraiLogger(): MiraiLogger { + return Log4jLogger(this); + } + + @JvmStatic + public fun org.slf4j.Logger.asMiraiLogger(): MiraiLogger { + return Slf4jLogger(this) + } +} \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/JdkLogger.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/JdkLogger.kt new file mode 100644 index 000000000..415f19e54 --- /dev/null +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/JdkLogger.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.utils.internal.logging + +import net.mamoe.mirai.utils.MiraiLoggerPlatformBase +import java.util.logging.Level +import java.util.logging.Logger + +internal class JdkLogger(private val logger: Logger) : MiraiLoggerPlatformBase() { + override fun verbose0(message: String?, e: Throwable?) { + logger.log(Level.FINER, message, e) + } + + override fun debug0(message: String?, e: Throwable?) { + logger.log(Level.FINEST, message, e) + + } + + override fun info0(message: String?, e: Throwable?) { + logger.log(Level.INFO, message, e) + } + + override fun warning0(message: String?, e: Throwable?) { + logger.log(Level.WARNING, message, e) + } + + override fun error0(message: String?, e: Throwable?) { + logger.log(Level.SEVERE, message, e) + } + + override val identity: String? + get() = logger.name +} \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Log4jLogger.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Log4jLogger.kt new file mode 100644 index 000000000..eb27bb42e --- /dev/null +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Log4jLogger.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.utils.internal.logging + +import net.mamoe.mirai.utils.MiraiLoggerPlatformBase +import org.apache.logging.log4j.Logger + +internal class Log4jLogger(private val logger: Logger) : MiraiLoggerPlatformBase() { + + override fun verbose0(message: String?, e: Throwable?) { + logger.trace(message, e) + } + + override fun debug0(message: String?, e: Throwable?) { + logger.debug(message, e) + } + + override fun info0(message: String?, e: Throwable?) { + logger.info(message, e) + } + + override fun warning0(message: String?, e: Throwable?) { + logger.warn(message, e) + } + + override fun error0(message: String?, e: Throwable?) { + logger.error(message, e) + } + + override val identity: String? + get() = logger.name + +} \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Slf4jLogger.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Slf4jLogger.kt new file mode 100644 index 000000000..2aead65c6 --- /dev/null +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/logging/Slf4jLogger.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.utils.internal.logging + +import net.mamoe.mirai.utils.MiraiLoggerPlatformBase +import org.slf4j.Logger + +internal class Slf4jLogger(private val logger: Logger) : MiraiLoggerPlatformBase() { + override fun verbose0(message: String?, e: Throwable?) { + logger.trace(message, e) + } + + override fun debug0(message: String?, e: Throwable?) { + logger.debug(message, e) + } + + override fun info0(message: String?, e: Throwable?) { + logger.info(message, e) + } + + override fun warning0(message: String?, e: Throwable?) { + logger.warn(message, e) + } + + override fun error0(message: String?, e: Throwable?) { + logger.error(message, e) + } + + override val identity: String? + get() = logger.name +} \ No newline at end of file