MiraiLog4JAdapterTest.kt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2019-2022 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. @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  10. package net.mamoe.mirai.utils.logging
  11. import io.ktor.client.*
  12. import io.ktor.client.engine.okhttp.*
  13. import net.mamoe.mirai.utils.MiraiLogger
  14. import net.mamoe.mirai.utils.loadService
  15. import java.io.ByteArrayOutputStream
  16. import java.io.PrintStream
  17. import kotlin.test.Test
  18. import kotlin.test.assertFalse
  19. import kotlin.test.assertIs
  20. internal class MiraiLog4JAdapterTest {
  21. @Test
  22. fun `using log4j`() {
  23. assertIs<MiraiLog4JFactory>(loadService(MiraiLogger.Factory::class))
  24. val logger = MiraiLogger.Factory.create(this::class)
  25. @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  26. assertIs<net.mamoe.mirai.internal.utils.Log4jLoggerAdapter>(logger)
  27. }
  28. @Test
  29. fun `print test`() {
  30. val out = ByteArrayOutputStream()
  31. System.setOut(PrintStream(out, true))
  32. System.setErr(PrintStream(out, true))
  33. HttpClient(OkHttp)
  34. val logger = MiraiLogger.Factory.create(this::class)
  35. logger.error("Hi")
  36. /*
  37. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  38. SLF4J: Defaulting to no-operation (NOP) logger implementation
  39. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  40. ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
  41. */
  42. out.flush()
  43. println(out.toString())
  44. assertFalse { out.toString().contains("slf4j", ignoreCase = true) }
  45. assertFalse { out.toString().contains("Log4j2 could not find a logging implementation", ignoreCase = true) }
  46. }
  47. }