Skip to content

Commit

Permalink
Merge pull request #1276 from Grryum/update/doobie
Browse files Browse the repository at this point in the history
Update doobie dependency with new effectfull LogHandler[F[_]] and remove tofu embed log handler.
  • Loading branch information
dos65 authored May 14, 2024
2 parents 6075a37 + 892e828 commit 7230837
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package tofu.example.doobie

import cats.data.ReaderT
import cats.effect.std.Dispatcher
import cats.effect.{Async, IO, IOApp, Sync}
import cats.effect.{Async, IO, IOApp}
import cats.tagless.syntax.functorK._
import cats.{Apply, Monad}
import derevo.derive
import doobie._
import doobie.implicits._
import doobie.util.log.LogHandler
import tofu.doobie.LiftConnectionIO
import tofu.doobie.log.{EmbeddableLogHandler, LogHandlerF}
import tofu.doobie.transactor.Txr
import tofu.higherKind.RepresentableK
import tofu.higherKind.derived.representableK
import tofu.kernel.types.PerformThrow
import tofu.lift.Lift
import tofu.logging.derivation.{loggable, loggingMidTry}
import tofu.logging.{Logging, LoggingCompanion}
Expand Down Expand Up @@ -45,11 +44,10 @@ trait PersonSql[F[_]] {
}

object PersonSql extends LoggingCompanion[PersonSql] {
def make[DB[_]: Monad: LiftConnectionIO: EmbeddableLogHandler]: PersonSql[DB] = {
EmbeddableLogHandler[DB].embedLift(implicit lh => new Impl)
}
def make[DB[_]: Monad: LiftConnectionIO]: PersonSql[DB] =
RepresentableK[PersonSql].mapK(Impl)(Lift[ConnectionIO, DB].liftF)

final class Impl(implicit lh: LogHandler) extends PersonSql[ConnectionIO] {
final private object Impl extends PersonSql[ConnectionIO] {
def init: ConnectionIO[Unit] =
lsql"create table if not exists person(id int8, name varchar(50), dept_id int8)".update.run.void
def create(p: Person): ConnectionIO[Unit] =
Expand All @@ -70,11 +68,10 @@ trait DeptSql[F[_]] {
}

object DeptSql extends LoggingCompanion[DeptSql] {
def make[DB[_]: Monad: LiftConnectionIO: EmbeddableLogHandler]: DeptSql[DB] = {
EmbeddableLogHandler[DB].embedLift(implicit lh => new Impl)
}
def make[DB[_]: Monad: LiftConnectionIO]: DeptSql[DB] =
RepresentableK[DeptSql].mapK(Impl)(Lift[ConnectionIO, DB].liftF)

final class Impl(implicit lh: LogHandler) extends DeptSql[ConnectionIO] {
final private object Impl extends DeptSql[ConnectionIO] {
def init: ConnectionIO[Unit] =
lsql"create table if not exists department(id int8, name varchar(50))".update.run.void
def create(d: Dept): ConnectionIO[Unit] =
Expand Down Expand Up @@ -118,23 +115,22 @@ object TofuDoobieExample extends IOApp.Simple {
runF[IO, ReaderT[IO, Ctx, *]]
}

def runF[I[_]: Async, F[_]: Sync: PerformThrow: WithRun[*[_], I, Ctx]]: I[Unit] = {
def runF[I[_], F[_]: Async: WithRun[*[_], I, Ctx]]: I[Unit] = {
// Simplified wiring below
implicit val loggingF = Logging.Make.contextual[F, Ctx]

val transactor = Transactor.fromDriverManager[I](
val transactor = Transactor.fromDriverManager[F](
driver = "org.h2.Driver",
url = "jdbc:h2:./test"
url = "jdbc:h2:./test",
logHandler = Some(LogHandler.loggable[F](Logging.Debug))
)
implicit val txr = Txr.continuational(transactor.mapK(Lift.trans[I, F]))
implicit val txr = Txr.continuational(transactor)

def initStorage[
DB[_]: Tries: Txr[F, *[_]]: Delay: Monad: LiftConnectionIO: WithLocal[*[_], Ctx]: PerformThrow
DB[_]: Tries: Txr[F, *[_]]: Delay: Monad: LiftConnectionIO: WithLocal[*[_], Ctx]
]: PersonStorage[F] = {
implicit val loggingDB = Logging.Make.contextual[DB, Ctx]

implicit val elh = EmbeddableLogHandler.async(LogHandlerF.loggable[DB](Logging.Debug))

val personSql = PersonSql.make[DB].attachErrLogs
val deptSql = DeptSql.make[DB].attachErrLogs

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,56 @@ object instances {
}

def logShow(ev: LogEvent): String = ev match {
case Success(s, a, e1, e2) =>
case Success(s, a, l, e1, e2) =>
s"""Successful Statement Execution:
|
| ${multiline(s)}
|
| arguments = ${loggedArgs(a)}
| label = $l
| elapsed = ${e1.toMillis} ms exec + ${e2.toMillis} ms processing (${(e1 + e2).toMillis} ms total)
""".stripMargin
case ProcessingFailure(s, a, e1, e2, _) =>
case ProcessingFailure(s, a, l, e1, e2, _) =>
s"""Failed Resultset Processing:
|
| ${multiline(s)}
|
| arguments = ${loggedArgs(a)}
| label = $l
| elapsed = ${e1.toMillis} ms exec + ${e2.toMillis} ms processing (failed) (${(e1 + e2).toMillis} ms total)
""".stripMargin
case ExecFailure(s, a, e1, _) =>
case ExecFailure(s, a, l, e1, _) =>
s"""Failed Statement Execution:
|
| ${multiline(s)}
|
| arguments = ${loggedArgs(a)}
| label = $l
| elapsed = ${e1.toMillis} ms exec (failed)
""".stripMargin
}

def fields[I, V, R, S](ev: LogEvent, i: I)(implicit r: LogRenderer[I, V, R, S]): R =
ev match {
case Success(s, a, e1, e2) =>
case Success(s, a, l, e1, e2) =>
i.field("sql-event-type", "Success") |+|
i.field("sql-label", l) |+|
i.field("sql-statement", oneline(s)) |+|
i.field("sql-args", loggedArgs(a)) |+|
i.field("sql-exec-ms", e1.toMillis) |+|
i.field("sql-processing-ms", e2.toMillis) |+|
i.field("sql-total-ms", (e1 + e2).toMillis)
case ProcessingFailure(s, a, e1, e2, _) =>
case ProcessingFailure(s, a, l, e1, e2, _) =>
i.field("sql-event-type", "ProcessingFailure") |+|
i.field("sql-label", l) |+|
i.field("sql-statement", oneline(s)) |+|
i.field("sql-args", loggedArgs(a)) |+|
i.field("sql-exec-ms", e1.toMillis) |+|
i.field("sql-processing-ms", e2.toMillis) |+|
i.field("sql-total-ms", (e1 + e2).toMillis)
case ExecFailure(s, a, e1, _) =>
case ExecFailure(s, a, l, e1, _) =>
i.field("sql-event-type", "ExecFailure") |+|
i.field("sql-label", l) |+|
i.field("sql-statement", oneline(s)) |+|
i.field("sql-args", loggedArgs(a)) |+|
i.field("sql-exec-ms", e1.toMillis)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package tofu.syntax.doobie.log

import doobie.util.log._
import tofu.doobie.log.LogHandlerF
import tofu.doobie.log.instances._
import tofu.kernel.types.AnyK
import tofu.logging.Logging

object handler {
implicit class MkLogHandlerF(private val lhf: LogHandlerF.type) extends AnyVal {
def loggable[F[_]: Logging.Make](level: Logging.Level): LogHandlerF[F] = { evt =>
val logging: Logging[F] = Logging.Make[F].forService[LogHandlerF[AnyK]]
implicit class MkLogHandlerF(private val lhf: LogHandler.type) extends AnyVal {
def loggable[F[_]: Logging.Make](level: Logging.Level): LogHandler[F] = { evt =>
val logging: Logging[F] = Logging.Make[F].forService[LogHandler[AnyK]]
evt match {
case _: Success => logging.write(level, "{}", evt)
case e: ProcessingFailure => logging.errorCause("{}", e.failure, evt)
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object Dependencies {

val doobie = "0.13.4"

val doobieCE3 = "1.0.0-RC2"
val doobieCE3 = "1.0.0-RC5"

// Compile time only
val macroParadise = "2.1.1"
Expand Down

0 comments on commit 7230837

Please sign in to comment.