Skip to content

Commit

Permalink
add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouyang committed Aug 30, 2020
1 parent bcbb265 commit b2c3e3f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/scala/com/your/domain/http4sexample/resource/logger.scala
@@ -0,0 +1,11 @@
package com.your.domain.http4sexample
package resource

import cats.Show
import cats.syntax.all._
import com.twitter.logging.Logger

trait HasLogger {
def logInfo[A: Show](text: A)(implicit logger: Logger): App[Unit] =
logger.info(text.show).pure[App]
}
Expand Up @@ -2,7 +2,7 @@ package com.your.domain.http4sexample

import cats.effect._

trait AppResource extends HasConfig with resource.HasClient with resource.HasDatabase
trait AppResource extends HasConfig with resource.HasClient with resource.HasDatabase with resource.HasLogger

package object resource {
def mk(implicit ctx: ContextShift[IO]): Resource[IO, AppResource] =
Expand Down
7 changes: 5 additions & 2 deletions src/main/scala/com/your/domain/http4sexample/route/joke.scala
Expand Up @@ -12,8 +12,10 @@ import io.circe.generic.auto._
import java.time.Instant
import java.time.ZonedDateTime
import java.time.ZoneId
import com.twitter.logging.Logger

object Joke {
implicit val log = Logger.get
case class Joke(joke: String)
val random = AppRoute {
case GET -> Root / "random-joke" =>
Expand All @@ -37,13 +39,14 @@ object Joke {
val CRUD = AppRoute {
case req @ POST -> Root / "joke" =>
for {
db <- Kleisli.ask[IO, HasDatabase]
has <- Kleisli.ask[IO, HasDatabase with HasLogger]
joke <- Kleisli.liftF(req.as[Repr.Create])
id <- db.transact(run(quote {
id <- has.transact(run(quote {
query[Dao.Joke]
.insert(_.text -> lift(joke.text))
.returningGenerated(_.id)
}))
_ <- has.logInfo(s"created joke with id $id")
resp <- Created(id)
} yield resp

Expand Down

0 comments on commit b2c3e3f

Please sign in to comment.