MiraiSlf4JSimpleAdapterTest.kt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2019-2021 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. package net.mamoe.mirai.utils.logging
  10. import io.ktor.client.*
  11. import io.ktor.client.engine.okhttp.*
  12. import net.mamoe.mirai.utils.MiraiLogger
  13. import net.mamoe.mirai.utils.loadService
  14. import org.junit.jupiter.api.Order
  15. import org.slf4j.LoggerFactory
  16. import org.slf4j.helpers.NOPLogger
  17. import java.io.ByteArrayOutputStream
  18. import java.io.PrintStream
  19. import kotlin.test.Test
  20. import kotlin.test.assertFalse
  21. import kotlin.test.assertIs
  22. import kotlin.test.assertIsNot
  23. internal class MiraiSlf4JSimpleAdapterTest {
  24. @Order(1)
  25. @Test
  26. fun `using log4j`() {
  27. assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
  28. val logger = MiraiLogger.Factory.create(this::class)
  29. @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  30. assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
  31. }
  32. @Order(0)
  33. @Test
  34. fun `print test`() {
  35. val out = ByteArrayOutputStream()
  36. System.setOut(PrintStream(out, true))
  37. System.setErr(PrintStream(out, true))
  38. assertIsNot<NOPLogger>(LoggerFactory.getLogger("s"))
  39. HttpClient(OkHttp)
  40. val logger = MiraiLogger.Factory.create(this::class)
  41. logger.error("Hi")
  42. /*
  43. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  44. SLF4J: Defaulting to no-operation (NOP) logger implementation
  45. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  46. ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
  47. */
  48. out.flush()
  49. println(out.toString())
  50. assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
  51. assertFalse {
  52. out.toString().contains("SLF4J: Defaulting to no-operation (NOP) logger implementation", ignoreCase = true)
  53. }
  54. }
  55. }