You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package org.slf4j.impl
  2. import scala.concurrent.ExecutionContext
  3. import scala.concurrent.duration._
  4. import _root_.monix.execution.Scheduler
  5. import cats.effect.Clock
  6. import cats.effect.ContextShift
  7. import cats.effect.Effect
  8. import cats.effect.IO
  9. import cats.effect.Timer
  10. import cats.implicits._
  11. import io.odin._
  12. import io.odin.json.Formatter
  13. import io.odin.slf4j.OdinLoggerBinder
  14. import io.odin.syntax._
  15. //effect type should be specified inbefore
  16. //log line will be recorded right after the call with no suspension
  17. class StaticLoggerBinder extends OdinLoggerBinder[IO] {
  18. val ec: ExecutionContext = Scheduler.global
  19. implicit val timer: Timer[IO] = IO.timer(ec)
  20. implicit val clock: Clock[IO] = timer.clock
  21. implicit val cs: ContextShift[IO] = IO.contextShift(ec)
  22. implicit val F: Effect[IO] = IO.ioEffect
  23. // val loggers: PartialFunction[String, Logger[IO]] = {
  24. // case "some.external.package.SpecificClass" =>
  25. // consoleLogger[IO](minLevel = Level.Warn) //disable noisy external logs
  26. // case asyncHttpClient
  27. // if asyncHttpClient.startsWith("org.asynchttpclient.netty") =>
  28. // consoleLogger[IO](minLevel = Level.Warn)
  29. // case _ => //if wildcard case isn't provided, default logger is no-op
  30. // consoleLogger[IO]()
  31. // }
  32. // private lazy val (defaultConsoleLogger, release1) =
  33. // consoleLogger[IO](minLevel = Level.Debug)
  34. // .withAsync(timeWindow = 1.milliseconds)
  35. // .allocated
  36. // .unsafeRunSync()
  37. private lazy val (mainFileLogger, release2) =
  38. fileLogger[IO](
  39. "application-log-2.log",
  40. Formatter.json,
  41. minLevel = Level.Debug
  42. ).withAsync(timeWindow = 1.milliseconds)
  43. .allocated
  44. .unsafeRunSync()
  45. sys.addShutdownHook(release2.unsafeRunSync())
  46. // private lazy val (eventBusFileLogger, release3) =
  47. // fileLogger[IO](
  48. // "eventbus.log",
  49. // Formatter.json,
  50. // minLevel = Level.Debug
  51. // ).withAsync(timeWindow = 1.milliseconds)
  52. // .allocated
  53. // .unsafeRunSync()
  54. // {
  55. // ArraySeq(release1, release2, release3).foreach(r =>
  56. // sys.addShutdownHook(r.unsafeRunSync())
  57. // )
  58. // }
  59. val loggers: PartialFunction[String, Logger[IO]] = {
  60. case "some.external.package.SpecificClass" =>
  61. //disable noisy external logs
  62. consoleLogger[IO](minLevel = Level.Debug).withMinimalLevel(Level.Warn)
  63. case asyncHttpClient
  64. if asyncHttpClient.startsWith("org.asynchttpclient.netty") =>
  65. consoleLogger[IO](minLevel = Level.Debug).withMinimalLevel(Level.Warn)
  66. // case s
  67. // if s.startsWith(
  68. // "wow.doge.mygame.subsystems.movement.PlayerMovementEventHandler"
  69. // ) =>
  70. // consoleLogger[IO](minLevel = Level.Debug).withMinimalLevel( Level.Trace) //selectively turn on trace logging for specific classes
  71. // case s if s.startsWith("wow.doge.mygame.events.EventBus") =>
  72. // consoleLogger[IO](minLevel = Level.Debug).withMinimalLevel(Level.Debug) |+| eventBusFileLogger
  73. case s if s.startsWith("akka.actor") || s.startsWith("nova.monadic_sfx") =>
  74. consoleLogger[IO](minLevel = Level.Debug)
  75. .withMinimalLevel(Level.Debug) |+| mainFileLogger
  76. case _ => //if wildcard case isn't provided, default logger is no-op
  77. consoleLogger[IO](minLevel = Level.Debug).withMinimalLevel(Level.Debug)
  78. }
  79. }
  80. object StaticLoggerBinder extends StaticLoggerBinder {
  81. val REQUESTED_API_VERSION: String = "1.7"
  82. def getSingleton: StaticLoggerBinder = this
  83. }