MiraiLog4JAdapterTest.kt 2.4 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 java.io.ByteArrayOutputStream
  15. import java.io.PrintStream
  16. import kotlin.test.Test
  17. import kotlin.test.assertFalse
  18. import kotlin.test.assertIs
  19. internal class MiraiLog4JAdapterTest {
  20. @Suppress("DEPRECATION")
  21. @Test
  22. fun `services prevail than legacy overrides`() {
  23. MiraiLogger.setDefaultLoggerCreator {
  24. net.mamoe.mirai.utils.SimpleLogger("my logger") { _: String?, _: Throwable? -> }
  25. }
  26. @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  27. assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(MiraiLogger.Factory.create(this::class))
  28. }
  29. @Test
  30. fun `using log4j`() {
  31. assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
  32. val logger = MiraiLogger.Factory.create(this::class)
  33. @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  34. assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
  35. }
  36. @Test
  37. fun `print test`() {
  38. val out = ByteArrayOutputStream()
  39. System.setOut(PrintStream(out, true))
  40. System.setErr(PrintStream(out, true))
  41. HttpClient(OkHttp)
  42. val logger = MiraiLogger.Factory.create(this::class)
  43. logger.error("Hi")
  44. /*
  45. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  46. SLF4J: Defaulting to no-operation (NOP) logger implementation
  47. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  48. ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
  49. */
  50. out.flush()
  51. println(out.toString())
  52. assertFalse { out.toString().contains("slf4j", ignoreCase = true) }
  53. assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
  54. }
  55. }