Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
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.

106 lines
3.5 KiB

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