Răsfoiți Sursa

日志系统可以重定向为log4j等jvm日志系统 (#498)

* Close #395

* LoggerAdapters
Karlatemp 5 ani în urmă
părinte
comite
39775c2fa0

+ 4 - 0
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")

+ 2 - 0
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))

+ 32 - 0
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)
+    }
+}

+ 40 - 0
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
+}

+ 40 - 0
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
+
+}

+ 38 - 0
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
+}