jmonkey-test/src/main/scala/org/slf4j/impl/StaticLoggerBuilder.scala

89 lines
2.9 KiB
Scala
Raw Normal View History

2020-11-13 14:14:57 +00:00
package org.slf4j.impl
import cats.effect.{ContextShift, Clock, Effect, IO, Timer}
import io.odin._
import io.odin.slf4j.OdinLoggerBinder
import cats.implicits._
import scala.concurrent.ExecutionContext
import _root_.monix.execution.Scheduler
import cats.arrow.FunctionK
import _root_.monix.execution.Scheduler.Implicits.global
import io.odin.syntax._
import scala.concurrent.duration._
import scala.collection.immutable.ArraySeq
import io.odin.json.Formatter
2020-11-13 14:14:57 +00:00
//effect type should be specified inbefore
//log line will be recorded right after the call with no suspension
class StaticLoggerBinder extends OdinLoggerBinder[IO] {
val ec: ExecutionContext = Scheduler.global
implicit val timer: Timer[IO] = IO.timer(ec)
implicit val clock: Clock[IO] = timer.clock
implicit val cs: ContextShift[IO] = IO.contextShift(ec)
implicit val F: Effect[IO] = IO.ioEffect
val monixToCats = new FunctionK[_root_.monix.bio.Task, IO] {
def apply[A](fa: _root_.monix.bio.Task[A]): IO[A] = fa.to[IO]
}
private lazy val (defaultConsoleLogger, release1) =
consoleLogger[IO](minLevel = Level.Debug)
.withAsync(timeWindow = 1.milliseconds)
.allocated
.unsafeRunSync()
private lazy val (mainFileLogger, release2) =
fileLogger[IO](
"application-log-2.log",
Formatter.json,
minLevel = Level.Debug
).withAsync(timeWindow = 1.milliseconds)
.allocated
.unsafeRunSync()
private lazy val (eventBusFileLogger, release3) =
2020-11-13 14:14:57 +00:00
fileLogger[IO](
"eventbus.log",
Formatter.json,
minLevel = Level.Debug
).withAsync(timeWindow = 1.milliseconds)
.allocated
.unsafeRunSync()
{
ArraySeq(release1, release2, release3).foreach(r =>
sys.addShutdownHook(r.unsafeRunSync())
)
}
2020-11-13 14:14:57 +00:00
val loggers: PartialFunction[String, Logger[IO]] = {
case "some.external.package.SpecificClass" =>
//disable noisy external logs
defaultConsoleLogger.withMinimalLevel(Level.Warn)
2020-11-13 14:14:57 +00:00
case asyncHttpClient
if asyncHttpClient.startsWith("org.asynchttpclient.netty") =>
defaultConsoleLogger.withMinimalLevel(Level.Warn)
// case s
// if s.startsWith(
// "wow.doge.mygame.subsystems.movement.PlayerMovementEventHandler"
// ) =>
// defaultConsoleLogger.withMinimalLevel( Level.Trace) //selectively turn on trace logging for specific classes
case s if s.startsWith("wow.doge.mygame.events.EventBus") =>
defaultConsoleLogger.withMinimalLevel(Level.Debug) |+| eventBusFileLogger
2020-11-13 14:14:57 +00:00
case s if s.startsWith("akka.actor") || s.startsWith("wow.doge.mygame") =>
defaultConsoleLogger.withMinimalLevel(Level.Debug) |+| mainFileLogger
2020-11-13 14:14:57 +00:00
case _ => //if wildcard case isn't provided, default logger is no-op
defaultConsoleLogger.withMinimalLevel(Level.Debug)
2020-11-13 14:14:57 +00:00
}
}
object StaticLoggerBinder extends StaticLoggerBinder {
var REQUESTED_API_VERSION: String = "1.7"
def getSingleton: StaticLoggerBinder = this
}