Skip to content

Commit

Permalink
Only log request/reponse when errors
Browse files Browse the repository at this point in the history
Add config to show every requests
  • Loading branch information
lenguyenthanh committed May 11, 2024
1 parent b2d9eb0 commit 03ace0e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
9 changes: 5 additions & 4 deletions modules/app/src/main/scala/app.config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ case class AppConfig(
elastic: ElasticConfig
)

case class HttpServerConfig(host: Host, port: Port, shutdownTimeout: Int)
case class HttpServerConfig(host: Host, port: Port, apiLogger: Boolean, shutdownTimeout: Int)

object HttpServerConfig:
private def host = env("HTTP_HOST").or(prop("http.host")).as[Host].default(ip"0.0.0.0")
private def port = env("HTTP_PORT").or(prop("http.port")).as[Port].default(port"9673")
private def host = env("HTTP_HOST").or(prop("http.host")).as[Host].default(ip"0.0.0.0")
private def port = env("HTTP_PORT").or(prop("http.port")).as[Port].default(port"9673")
private def logger = env("HTTP_API_LOGGER").or(prop("http.api.logger")).as[Boolean].default(false)
private def shutdownTimeout =
env("HTTP_SHUTDOWN_TIMEOUT").or(prop("http.shutdown.timeout")).as[Int].default(30)
def config = (host, port, shutdownTimeout).parMapN(HttpServerConfig.apply)
def config = (host, port, logger, shutdownTimeout).parMapN(HttpServerConfig.apply)

case class ElasticConfig(uri: String)

Expand Down
2 changes: 1 addition & 1 deletion modules/app/src/main/scala/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object App extends IOApp.Simple:
class SearchApp(res: AppResources, config: AppConfig)(using Logger[IO]):
def run(): Resource[IO, Unit] =
for
httpApp <- Routes(res)
httpApp <- Routes(res, config.server)
server <- MkHttpServer.apply.newEmber(config.server, httpApp)
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource
yield ()
23 changes: 23 additions & 0 deletions modules/app/src/main/scala/http.logger.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lila.search
package app

import cats.data.Kleisli
import cats.effect.IO
import cats.syntax.all.*
import org.http4s.internal.Logger as Http4sLogger
import org.http4s.{ HttpApp, Request, Response }
import org.typelevel.log4cats.Logger

object ApiErrorLogger:

def isResponseError(res: Response[IO]): Boolean =
!res.status.isSuccess && res.status.code != 404

private def logError(req: Request[IO], res: Response[IO])(using Logger[IO]): IO[Unit] =
Http4sLogger.logMessage(req)(true, true)(Logger[IO].warn) >>
Http4sLogger.logMessage(res)(true, true)(Logger[IO].warn)

def instance(using Logger[IO]): HttpApp[IO] => HttpApp[IO] = http =>
Kleisli: req =>
http(req).flatTap: res =>
logError(req, res).whenA(isResponseError(res))
10 changes: 8 additions & 2 deletions modules/app/src/main/scala/http.middleware.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import cats.effect.IO
import org.http4s.*
import org.http4s.implicits.*
import org.http4s.server.middleware.*
import org.typelevel.log4cats.Logger

import scala.concurrent.duration.*

type Middleware = HttpRoutes[IO] => HttpRoutes[IO]
def ApplyMiddleware(routes: HttpRoutes[IO]): HttpApp[IO] =
def ApplyMiddleware(config: HttpServerConfig)(routes: HttpRoutes[IO])(using Logger[IO]): HttpApp[IO] =

val autoSlash: Middleware = AutoSlash(_)
val timeout: Middleware = Timeout(60.seconds)

val middleware = autoSlash.andThen(timeout)
val logger =

def verboseLogger =
RequestLogger.httpApp[IO](true, true) andThen
ResponseLogger.httpApp[IO, Request[IO]](true, true)

val logger =
if config.apiLogger then verboseLogger
else ApiErrorLogger.instance

logger(middleware(routes).orNotFound)
4 changes: 2 additions & 2 deletions modules/app/src/main/scala/http.routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.http4s.{ HttpApp, HttpRoutes }
import org.typelevel.log4cats.Logger
import smithy4s.http4s.SimpleRestJsonBuilder

def Routes(resources: AppResources)(using Logger[IO]): Resource[IO, HttpApp[IO]] =
def Routes(resources: AppResources, config: HttpServerConfig)(using Logger[IO]): Resource[IO, HttpApp[IO]] =

val healthServiceImpl: HealthService[IO] = new HealthService.Default[IO](IO.stub)

Expand All @@ -28,4 +28,4 @@ def Routes(resources: AppResources)(using Logger[IO]): Resource[IO, HttpApp[IO]]
.sequence
.map(_.reduceK)
.map(_ <+> docs)
.map(ApplyMiddleware)
.map(ApplyMiddleware(config))
2 changes: 1 addition & 1 deletion modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object CompatSuite extends weaver.IOSuite:
IO.fromFuture(IO(client.storeBulkTeam(sources))).map(expect.same(_, ()))

def testAppConfig = AppConfig(
server = HttpServerConfig(ip"0.0.0.0", port"9999", shutdownTimeout = 1),
server = HttpServerConfig(ip"0.0.0.0", port"9999", false, shutdownTimeout = 1),
elastic = ElasticConfig("http://0.0.0.0:9200")
)

Expand Down

0 comments on commit 03ace0e

Please sign in to comment.