Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging #258

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/scala/Work.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 24 additions & 0 deletions app/src/main/scala/http/ApiLogger.scala
Original file line number Diff line number Diff line change
@@ -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
)
21 changes: 14 additions & 7 deletions app/src/main/scala/http/HttpApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
7 changes: 3 additions & 4 deletions app/src/main/scala/http/MkHttpServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}")