diff --git a/app/src/main/scala/Work.scala b/app/src/main/scala/Work.scala index a81960f..60ac52f 100644 --- a/app/src/main/scala/Work.scala +++ b/app/src/main/scala/Work.scala @@ -47,6 +47,6 @@ object Work: def similar(to: Move) = request.id == to.request.id && request.moves == to.request.moves override def toString = - s"id:$id game:${request.id} variant:${request.variant.key} level:${request.level} tries:$tries created:$createdAt acquired:$acquired" + s"id:$id game:${request.id} variant:${request.variant.key} level:${request.level} tries:$tries created:$createdAt acquired:$acquired move: ${request.moves}" def makeId = WorkId(scala.util.Random.alphanumeric.take(8).mkString) diff --git a/app/src/main/scala/http/ApiLogger.scala b/app/src/main/scala/http/ApiLogger.scala new file mode 100644 index 0000000..11ff634 --- /dev/null +++ b/app/src/main/scala/http/ApiLogger.scala @@ -0,0 +1,24 @@ +package lila.fishnet +package http + +import cats.data.Kleisli +import cats.syntax.all.* +import cats.effect.IO +import org.http4s.internal.Logger as Http4sLogger +import org.http4s.{ HttpApp, Response } +import org.typelevel.log4cats.Logger + +object ApiErrorLogger: + + val logOnError: Response[IO] => Boolean = res => !res.status.isSuccess && res.status.code != 404 + + def instance(using Logger[IO]): HttpApp[IO] => HttpApp[IO] = http => + Kleisli: req => + http(req).flatTap: res => + logOnError(res) + .pure[IO] + .ifM( + Http4sLogger.logMessage(req)(true, true)(Logger[IO].warn) >> + Http4sLogger.logMessage(res)(true, true)(Logger[IO].warn), + IO.unit + ) diff --git a/app/src/main/scala/http/HttpApi.scala b/app/src/main/scala/http/HttpApi.scala index 881a706..1f34157 100644 --- a/app/src/main/scala/http/HttpApi.scala +++ b/app/src/main/scala/http/HttpApi.scala @@ -8,8 +8,13 @@ import cats.effect.IO import org.http4s.* import org.http4s.implicits.* import org.http4s.server.middleware.* +import org.typelevel.log4cats.Logger -final class HttpApi(executor: Executor, healthCheck: HealthCheck, config: HttpServerConfig): +final class HttpApi( + executor: Executor, + healthCheck: HealthCheck, + config: HttpServerConfig +)(using Logger[IO]): private val fishnetRoutes = FishnetRoutes(executor).routes private val healthRoutes = HealthRoutes(healthCheck).routes @@ -23,11 +28,13 @@ final class HttpApi(executor: Executor, healthCheck: HealthCheck, config: HttpSe private val middleware = autoSlash andThen timeout - private val loggers: HttpApp[IO] => HttpApp[IO] = - RequestLogger.httpApp[IO](false, true) andThen - ResponseLogger.httpApp[IO, Request[IO]](false, true) + private def verboseLogger = + RequestLogger.httpApp[IO](true, true) andThen + ResponseLogger.httpApp[IO, Request[IO]](true, true) + + private val loggers = + if config.apiLogger then verboseLogger + else ApiErrorLogger.instance val httpApp: HttpApp[IO] = - val app = middleware(routes).orNotFound - if config.apiLogger then loggers(app) - else app + loggers(middleware(routes).orNotFound) diff --git a/app/src/main/scala/http/MkHttpServer.scala b/app/src/main/scala/http/MkHttpServer.scala index c8a5e17..b52a585 100644 --- a/app/src/main/scala/http/MkHttpServer.scala +++ b/app/src/main/scala/http/MkHttpServer.scala @@ -5,7 +5,6 @@ import cats.effect.{ IO, Resource } import fs2.io.net.Network import org.http4s.ember.server.EmberServerBuilder import org.http4s.server.Server -import org.http4s.server.defaults.Banner import org.typelevel.log4cats.Logger trait MkHttpServer: @@ -23,7 +22,7 @@ object MkHttpServer: .withPort(cfg.port) .withHttpApp(httpApp) .build - .evalTap(showEmberBanner) + .evalTap(showBanner) - private def showEmberBanner(s: Server): IO[Unit] = - Logger[IO].info(s"\n${Banner.mkString("\n")}\nHTTP Server started at ${s.address}") + private def showBanner(s: Server): IO[Unit] = + Logger[IO].info(s"Lila Fishnet started at ${s.address}")