mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-27 04:30:08 +08:00
Add adapter modules for logging libraries to connect with mirai logger
This commit is contained in:
parent
317b3cc0c7
commit
20a14fc716
@ -39,7 +39,8 @@ object Versions {
|
||||
|
||||
const val shadow = "6.1.0"
|
||||
|
||||
const val slf4j = "1.7.30"
|
||||
const val logback = "1.2.5"
|
||||
const val slf4j = "1.7.32"
|
||||
const val log4j = "2.14.1"
|
||||
const val asm = "9.1"
|
||||
const val difflib = "1.3.0"
|
||||
@ -86,11 +87,15 @@ val `ktor-client-logging` = ktor("client-logging", Versions.ktor)
|
||||
val `ktor-network` = ktor("network", Versions.ktor)
|
||||
val `ktor-client-serialization-jvm` = ktor("client-serialization-jvm", Versions.ktor)
|
||||
|
||||
const val `logback-classic` = "ch.qos.logback:logback-classic:" + Versions.logback
|
||||
|
||||
const val `slf4j-api` = "org.slf4j:slf4j-api:" + Versions.slf4j
|
||||
const val `slf4j-simple` = "org.slf4j:slf4j-simple:" + Versions.slf4j
|
||||
|
||||
const val `log4j-api` = "org.apache.logging.log4j:log4j-api:" + Versions.log4j
|
||||
const val `log4j-core` = "org.apache.logging.log4j:log4j-core:" + Versions.log4j
|
||||
const val `log4j-slf4j-impl` = "org.apache.logging.log4j:log4j-slf4j-impl:" + Versions.log4j
|
||||
const val `log4j-to-slf4j` = "org.apache.logging.log4j:log4j-to-slf4j:" + Versions.log4j
|
||||
|
||||
val ATTRIBUTE_MIRAI_TARGET_PLATFORM: Attribute<String> = Attribute.of("mirai.target.platform", String::class.java)
|
||||
|
||||
|
148
docs/Bots.md
148
docs/Bots.md
@ -173,6 +173,20 @@ setDeviceInfo(bot -> /* create device info */)
|
||||
|
||||
在线生成自定义设备信息的 `device.json`: https://ryoii.github.io/mirai-devicejs-generator/
|
||||
|
||||
#### 使用其他日志库接管 mirai 日志系统
|
||||
*mirai 2.7 起支持*
|
||||
|
||||
使用 Log4J, SLF4J 等接管 mirai 日志系统后则可使用它们的过滤等高级功能.
|
||||
|
||||
mirai 内部使用 Log4J2, 依赖 log4j-api. Log4J2 支持对接到 SLF4J 等.
|
||||
|
||||
- 要使用 Log4J2, 添加依赖 `net.mamoe:mirai-logging-log4j2`
|
||||
- 要使用 SLF4J 以及自定义的 SLF4J 实现, 添加依赖 `net.mamoe:mirai-logging-slf4j`
|
||||
- 要使用 SLF4J 以及 slf4j-simple, 添加依赖 `net.mamoe:mirai-logging-slf4j-simple`
|
||||
- 要使用 SLF4J 以及 logback-classic, 添加依赖 `net.mamoe:mirai-logging-slf4j-logback`
|
||||
|
||||
版本号都与 mirai-core 相同.
|
||||
|
||||
#### 重定向日志
|
||||
Bot 有两个日志类别,`Bot` 或 `Net`。`Bot` 为通常日志,如收到事件。`Net` 为网络日志,包含收到和发出的每一个包和网络层解析时遇到的错误。
|
||||
|
||||
@ -271,3 +285,137 @@ contactListCache.setSaveIntervalMillis(60000) // 可选设置有更新时的保
|
||||
> 下一步,[Contacts](Contacts.md)
|
||||
>
|
||||
> [回到 Mirai 文档索引](CoreAPI.md)
|
||||
|
||||
|
||||
<!--
|
||||
|
||||
## 附录
|
||||
|
||||
[Ktor]: https://github.com/ktor.io/ktor
|
||||
|
||||
### 使用 Log4J2 接管 mirai 日志系统
|
||||
|
||||
添加依赖:
|
||||
|
||||
| Group ID | Artifact ID | 最低版本号 | 说明 |
|
||||
|:------------------------:|:----------------:|:--------:|:---------------------------------------------|
|
||||
| org.apache.logging.log4j | log4j-api | 2.14.1 | Log4J2 API 模块, 将会覆盖 mirai 依赖的 log4j-api |
|
||||
| org.apache.logging.log4j | log4j-core | 2.14.1 | Log4J2 核心实现模块, 所有相关模块版本必须相同 |
|
||||
| org.apache.logging.log4j | log4j-slf4j-impl | 2.14.1 | 用于将对 SLF4J 的调用转换为对 Log4J 的调用 |
|
||||
|
||||
要添加 `log4j-slf4j-impl` 是因为 [Ktor] 使用了 SLF4J. 添加该模块可以让整个应用统一使用同一个日志库.
|
||||
|
||||
#### Gradle 示例
|
||||
|
||||
*`build.gradle.kts`*
|
||||
```kotlin
|
||||
dependencies {
|
||||
val log4jVersion = "2.14.1"
|
||||
implementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
|
||||
implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
|
||||
implementation("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion")
|
||||
|
||||
implementation("net.mamoe:mirai-core:2.7-RC") // 示例版本号
|
||||
}
|
||||
```
|
||||
|
||||
#### Maven 示例
|
||||
|
||||
*`pom.xml`*
|
||||
```xml
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.mamoe</groupId>
|
||||
<artifactId>mirai-core</artifactId>
|
||||
<version>2.7-RC</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
### 使用 SLF4J 接管 mirai 日志系统
|
||||
|
||||
添加依赖:
|
||||
|
||||
| Group ID | Artifact ID | 最低版本号 | 说明 |
|
||||
|:------------------------:|:--------------:|:--------:|:---------------------------------------------|
|
||||
| org.apache.logging.log4j | log4j-api | 2.14.1 | Log4J2 API 模块, 将会覆盖 mirai 依赖的 log4j-api |
|
||||
| org.apache.logging.log4j | log4j-to-slf4j | 2.14.1 | 用于将对 Log4J 的调用转换为对 SLF4J 的调用 |
|
||||
| org.slf4j | slf4j-api | 1.7.32 | SLF4J API 模块, 将会覆盖 [Ktor] 依赖的 slf4j-api |
|
||||
|
||||
除上以外, 还要添加一个 SLF4J 的实现, 例如 `slf4j-simple` 或 `logback-classic`.
|
||||
这与通常的操作方式相同.
|
||||
|
||||
#### Gradle 示例
|
||||
|
||||
*`build.gradle.kts`*
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("org.apache.logging.log4j:log4j-api:2.14.1")
|
||||
implementation("org.apache.logging.log4j:log4j-to-slf4j:2.14.1")
|
||||
implementation("org.slf4j:slf4j-api:1.7.32")
|
||||
|
||||
implementation("org.slf4j:slf4j-simple:1.7.32") // 若要使用 slf4j-simple
|
||||
implementation("ch.qos.logback:logback-classic:1.2.5") // 若要使用 logback
|
||||
|
||||
|
||||
implementation("net.mamoe:mirai-core:2.7-RC") // 示例版本号
|
||||
}
|
||||
```
|
||||
|
||||
#### Maven 示例
|
||||
|
||||
*`pom.xml`*
|
||||
```xml
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-to-slf4j</artifactId>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.32</version>
|
||||
</dependency>
|
||||
|
||||
<dependency> <!-- 若要使用 slf4j-simple --
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.32</version>
|
||||
</dependency>
|
||||
|
||||
<dependency> <!-- 若要使用 logback --
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.mamoe</groupId>
|
||||
<artifactId>mirai-core</artifactId>
|
||||
<version>2.7-RC</version> <!--示例版本号--
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
-->
|
37
logging/mirai-logging-log4j2/build.gradle.kts
Normal file
37
logging/mirai-logging-log4j2/build.gradle.kts
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("UnusedImport")
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("java")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
version = Versions.core
|
||||
description = "Mirai Log4J Adapter"
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":mirai-core-api"))
|
||||
api(`log4j-api`)
|
||||
api(`log4j-core`)
|
||||
api(`log4j-slf4j-impl`)
|
||||
|
||||
|
||||
testImplementation(`slf4j-api`)
|
||||
testImplementation(project(":mirai-core"))
|
||||
testImplementation(project(":mirai-core-utils"))
|
||||
}
|
||||
|
||||
configurePublishing("mirai-logging-log4j2")
|
@ -0,0 +1,10 @@
|
||||
#
|
||||
# Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
#
|
||||
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
#
|
||||
# https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
#
|
||||
|
||||
net.mamoe.mirai.utils.logging.MiraiLog4JFactory
|
30
logging/mirai-logging-log4j2/src/MiraiLog4JFactory.kt
Normal file
30
logging/mirai-logging-log4j2/src/MiraiLog4JFactory.kt
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils.logging
|
||||
|
||||
import net.mamoe.mirai.utils.MiraiInternalApi
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import org.apache.logging.log4j.MarkerManager
|
||||
|
||||
/**
|
||||
* 使用 Log4J 接管 mirai 日志系统.
|
||||
*/
|
||||
@MiraiInternalApi
|
||||
public class MiraiLog4JFactory : MiraiLogger.Factory {
|
||||
override fun create(requester: Class<*>, identity: String?): MiraiLogger {
|
||||
val logger = LogManager.getLogger(requester)
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
return net.mamoe.mirai.internal.utils.Log4jLoggerAdapter(
|
||||
logger,
|
||||
MarkerManager.getMarker(identity ?: logger.name).addParents(net.mamoe.mirai.internal.utils.MARKER_MIRAI)
|
||||
)
|
||||
}
|
||||
}
|
52
logging/mirai-logging-log4j2/test/MiraiLog4JAdapterTest.kt
Normal file
52
logging/mirai-logging-log4j2/test/MiraiLog4JAdapterTest.kt
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils.logging
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.okhttp.*
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.loadService
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertIs
|
||||
|
||||
internal class MiraiLog4JAdapterTest {
|
||||
|
||||
@Test
|
||||
fun `using log4j`() {
|
||||
assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `print test`() {
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out, true))
|
||||
System.setErr(PrintStream(out, true))
|
||||
HttpClient(OkHttp)
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
logger.error("Hi")
|
||||
/*
|
||||
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
|
||||
SLF4J: Defaulting to no-operation (NOP) logger implementation
|
||||
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
|
||||
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
|
||||
|
||||
*/
|
||||
out.flush()
|
||||
println(out.toString())
|
||||
assertFalse { out.toString().contains("slf4j", ignoreCase = true) }
|
||||
assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
|
||||
}
|
||||
}
|
38
logging/mirai-logging-slf4j-logback/build.gradle.kts
Normal file
38
logging/mirai-logging-slf4j-logback/build.gradle.kts
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("UnusedImport")
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("java")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
version = Versions.core
|
||||
description = "Mirai SLF4J Adapter with logback-classic"
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":mirai-core-api"))
|
||||
api(project(":mirai-logging-slf4j")) // mirai -> slf4j
|
||||
implementation(project(":mirai-logging-log4j2")) {
|
||||
exclude("org.apache.logging.log4j", "log4j-slf4j-impl")
|
||||
} // mirai -> log4j2
|
||||
api(`slf4j-api`)
|
||||
api(`logback-classic`)
|
||||
|
||||
testImplementation(project(":mirai-core"))
|
||||
testImplementation(project(":mirai-core-utils"))
|
||||
}
|
||||
|
||||
configurePublishing("mirai-logging-slf4j-logback")
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils.logging
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.okhttp.*
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.loadService
|
||||
import org.junit.jupiter.api.Order
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.slf4j.helpers.NOPLogger
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertIsNot
|
||||
|
||||
internal class MiraiSlf4JLogbackAdapterTest {
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
fun `using log4j`() {
|
||||
assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
|
||||
}
|
||||
|
||||
@Order(0)
|
||||
@Test
|
||||
fun `print test`() {
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out, true))
|
||||
System.setErr(PrintStream(out, true))
|
||||
|
||||
assertIsNot<NOPLogger>(LoggerFactory.getLogger("s"))
|
||||
HttpClient(OkHttp)
|
||||
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
logger.error("Hi")
|
||||
/*
|
||||
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
|
||||
SLF4J: Defaulting to no-operation (NOP) logger implementation
|
||||
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
|
||||
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
|
||||
|
||||
*/
|
||||
out.flush()
|
||||
println(out.toString())
|
||||
assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
|
||||
assertFalse {
|
||||
out.toString().contains("SLF4J: Defaulting to no-operation (NOP) logger implementation", ignoreCase = true)
|
||||
}
|
||||
}
|
||||
}
|
38
logging/mirai-logging-slf4j-simple/build.gradle.kts
Normal file
38
logging/mirai-logging-slf4j-simple/build.gradle.kts
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("UnusedImport")
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("java")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
version = Versions.core
|
||||
description = "Mirai SLF4J Adapter with slf4j-simple"
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":mirai-core-api"))
|
||||
api(project(":mirai-logging-slf4j")) // mirai -> slf4j
|
||||
implementation(project(":mirai-logging-log4j2")) {
|
||||
exclude("org.apache.logging.log4j", "log4j-slf4j-impl")
|
||||
} // mirai -> log4j2
|
||||
api(`slf4j-api`)
|
||||
api(`slf4j-simple`)
|
||||
|
||||
testImplementation(project(":mirai-core"))
|
||||
testImplementation(project(":mirai-core-utils"))
|
||||
}
|
||||
|
||||
configurePublishing("mirai-logging-slf4j-simple")
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils.logging
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.okhttp.*
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.loadService
|
||||
import org.junit.jupiter.api.Order
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.slf4j.helpers.NOPLogger
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertIsNot
|
||||
|
||||
internal class MiraiSlf4JSimpleAdapterTest {
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
fun `using log4j`() {
|
||||
assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
|
||||
}
|
||||
|
||||
@Order(0)
|
||||
@Test
|
||||
fun `print test`() {
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out, true))
|
||||
System.setErr(PrintStream(out, true))
|
||||
|
||||
assertIsNot<NOPLogger>(LoggerFactory.getLogger("s"))
|
||||
HttpClient(OkHttp)
|
||||
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
logger.error("Hi")
|
||||
/*
|
||||
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
|
||||
SLF4J: Defaulting to no-operation (NOP) logger implementation
|
||||
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
|
||||
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
|
||||
|
||||
*/
|
||||
out.flush()
|
||||
println(out.toString())
|
||||
assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
|
||||
assertFalse {
|
||||
out.toString().contains("SLF4J: Defaulting to no-operation (NOP) logger implementation", ignoreCase = true)
|
||||
}
|
||||
}
|
||||
}
|
37
logging/mirai-logging-slf4j/build.gradle.kts
Normal file
37
logging/mirai-logging-slf4j/build.gradle.kts
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("UnusedImport")
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("java")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
version = Versions.core
|
||||
description = "Mirai SLF4J Adapter"
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":mirai-core-api"))
|
||||
implementation(project(":mirai-logging-log4j2")) {
|
||||
exclude("org.apache.logging.log4j", "log4j-slf4j-impl")
|
||||
} // mirai -> log4j2
|
||||
implementation(`log4j-to-slf4j`) // log4j2 -> slf4j
|
||||
api(`slf4j-api`)
|
||||
|
||||
testImplementation(project(":mirai-core"))
|
||||
testImplementation(project(":mirai-core-utils"))
|
||||
}
|
||||
|
||||
configurePublishing("mirai-logging-slf4j")
|
62
logging/mirai-logging-slf4j/test/MiraiSlf4JAdapterTest.kt
Normal file
62
logging/mirai-logging-slf4j/test/MiraiSlf4JAdapterTest.kt
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils.logging
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.okhttp.*
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.loadService
|
||||
import org.junit.jupiter.api.Order
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.slf4j.helpers.NOPLogger
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertIs
|
||||
|
||||
internal class MiraiSlf4JAdapterTest {
|
||||
|
||||
@Order(1)
|
||||
@Test
|
||||
fun `using log4j`() {
|
||||
assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
|
||||
}
|
||||
|
||||
@Order(0)
|
||||
@Test
|
||||
fun `print test`() {
|
||||
val out = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(out, true))
|
||||
System.setErr(PrintStream(out, true))
|
||||
|
||||
assertIs<NOPLogger>(LoggerFactory.getLogger("s"))
|
||||
HttpClient(OkHttp)
|
||||
|
||||
val logger = MiraiLogger.Factory.create(this::class)
|
||||
logger.error("Hi")
|
||||
/*
|
||||
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
|
||||
SLF4J: Defaulting to no-operation (NOP) logger implementation
|
||||
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
|
||||
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
|
||||
|
||||
*/
|
||||
out.flush()
|
||||
println(out.toString())
|
||||
assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
|
||||
// assertTrue {
|
||||
// out.toString().contains("SLF4J: Defaulting to no-operation (NOP) logger implementation", ignoreCase = true)
|
||||
// }
|
||||
}
|
||||
}
|
@ -19,6 +19,11 @@ pluginManagement {
|
||||
|
||||
rootProject.name = "mirai"
|
||||
|
||||
fun includeProject(projectPath: String, dir: String? = null) {
|
||||
include(projectPath)
|
||||
if (dir != null) project(projectPath).projectDir = file(dir)
|
||||
}
|
||||
|
||||
include(":mirai-core-utils")
|
||||
include(":mirai-core-api")
|
||||
include(":mirai-core")
|
||||
@ -29,14 +34,17 @@ include(":binary-compatibility-validator-android")
|
||||
project(":binary-compatibility-validator-android").projectDir = file("binary-compatibility-validator/android")
|
||||
include(":ci-release-helper")
|
||||
|
||||
includeProject(":mirai-logging-log4j2", "logging/mirai-logging-log4j2")
|
||||
includeProject(":mirai-logging-slf4j", "logging/mirai-logging-slf4j")
|
||||
includeProject(":mirai-logging-slf4j-simple", "logging/mirai-logging-slf4j-simple")
|
||||
includeProject(":mirai-logging-slf4j-logback", "logging/mirai-logging-slf4j-logback")
|
||||
|
||||
|
||||
fun includeConsoleProjects() {
|
||||
val disableOldFrontEnds = true
|
||||
|
||||
fun includeConsoleProject(projectPath: String, path: String? = null) {
|
||||
include(projectPath)
|
||||
if (path != null) project(projectPath).projectDir = file("mirai-console/$path")
|
||||
}
|
||||
fun includeConsoleProject(projectPath: String, dir: String? = null) =
|
||||
includeProject(projectPath, "mirai-console/$dir")
|
||||
|
||||
includeConsoleProject(":mirai-console-compiler-annotations", "tools/compiler-annotations")
|
||||
includeConsoleProject(":mirai-console", "backend/mirai-console")
|
||||
|
Loading…
Reference in New Issue
Block a user