From ddd05b93aae696a92559523055e4a6a99b5fbe19 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 19 Jul 2023 00:10:14 +0200 Subject: [PATCH 1/4] Add module for log4cats support Creates a Log4CatsLogHandler class --- build.sbt | 13 +++++ .../doobie/log4cats/Log4CatsLogHandler.scala | 48 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala diff --git a/build.sbt b/build.sbt index a58f87d2b..d4e16169a 100644 --- a/build.sbt +++ b/build.sbt @@ -11,6 +11,7 @@ lazy val fs2Version = "3.7.0" lazy val h2Version = "1.4.200" lazy val hikariVersion = "5.0.1" // N.B. Hikari v4 introduces a breaking change via slf4j v2 lazy val kindProjectorVersion = "0.11.2" +lazy val log4catsVersion = "2.6.0" lazy val postGisVersion = "2.5.1" lazy val postgresVersion = "42.6.0" lazy val refinedVersion = "0.11.0" @@ -137,6 +138,7 @@ lazy val doobie = project.in(file(".")) h2, `h2-circe`, hikari, + log4cats, postgres, `postgres-circe`, refined, @@ -241,6 +243,17 @@ lazy val core = project }.taskValue ) +lazy val log4cats = project + .in(file("modules/log4cats")) + .enablePlugins(AutomateHeaderPlugin) + .dependsOn(core) + .settings(doobieSettings) + .settings( + name := "doobie-log4cats", + description := "log4cats support for doobie.", + libraryDependencies += "org.typelevel" %% "log4cats-core" % log4catsVersion + ) + lazy val example = project .in(file("modules/example")) .enablePlugins(NoPublishPlugin) diff --git a/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala b/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala new file mode 100644 index 000000000..596c7f5c8 --- /dev/null +++ b/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala @@ -0,0 +1,48 @@ +// Copyright (c) 2013-2020 Rob Norris and Contributors +// This software is licensed under the MIT License (MIT). +// For more information see LICENSE or https://opensource.org/licenses/MIT + +package doobie.log4cats + +import doobie.LogHandler +import doobie.util.log._ +import org.typelevel.log4cats._ + +class Log4CatsLogHandler[F[_]](logger: MessageLogger[F]) extends LogHandler[F]{ + override def run(logEvent: LogEvent): F[Unit] = logEvent match { + case Success(s, a, l, e1, e2) => + logger.info( + s"""Successful Statement Execution: + | + | ${s.linesIterator.dropWhile(_.trim.isEmpty).mkString("\n ")} + | + | arguments = [${a.mkString(", ")}] + | label = $l + | elapsed = ${e1.toMillis.toString} ms exec + ${e2.toMillis.toString} ms processing (${(e1 + e2).toMillis.toString} ms total) + """.stripMargin) + + case ProcessingFailure(s, a, l, e1, e2, t) => + logger.warn( + s"""Failed Resultset Processing: + | + | ${s.linesIterator.dropWhile(_.trim.isEmpty).mkString("\n ")} + | + | arguments = [${a.mkString(", ")}] + | label = $l + | elapsed = ${e1.toMillis.toString} ms exec + ${e2.toMillis.toString} ms processing (failed) (${(e1 + e2).toMillis.toString} ms total) + | failure = ${t.getMessage} + """.stripMargin) + + case ExecFailure(s, a, l, e1, t) => + logger.error( + s"""Failed Statement Execution: + | + | ${s.linesIterator.dropWhile(_.trim.isEmpty).mkString("\n ")} + | + | arguments = [${a.mkString(", ")}] + | label = $l + | elapsed = ${e1.toMillis.toString} ms exec (failed) + | failure = ${t.getMessage} + """.stripMargin) + } +} From 19f9e568d87122c7264d3b375cb355beac3784ce Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 19 Jul 2023 00:22:35 +0200 Subject: [PATCH 2/4] mergifyGenerate --- .mergify.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.mergify.yml b/.mergify.yml index ef7f4fefe..fb0f9e481 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -79,6 +79,14 @@ pull_request_rules: add: - hikari remove: [] +- name: Label log4cats PRs + conditions: + - files~=^modules/log4cats/ + actions: + label: + add: + - log4cats + remove: [] - name: Label munit PRs conditions: - files~=^modules/munit/ From 5e0d7096853b0fee3b3694ccd04fdce97424f341 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 19 Jul 2023 00:27:08 +0200 Subject: [PATCH 3/4] githubWorkflowGenerate --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 493c07ab0..29f26c764 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -94,11 +94,11 @@ jobs: - name: Make target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') - run: mkdir -p modules/weaver/target modules/scalatest/target modules/refined/target target modules/postgres/target modules/docs/target modules/postgres-circe/target modules/h2/target modules/hikari/target modules/munit/target modules/h2-circe/target modules/core/target modules/example/target modules/specs2/target modules/free/target modules/bench/target project/target + run: mkdir -p modules/weaver/target modules/scalatest/target modules/refined/target target modules/postgres/target modules/docs/target modules/log4cats/target modules/postgres-circe/target modules/h2/target modules/hikari/target modules/munit/target modules/h2-circe/target modules/core/target modules/example/target modules/specs2/target modules/free/target modules/bench/target project/target - name: Compress target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') - run: tar cf targets.tar modules/weaver/target modules/scalatest/target modules/refined/target target modules/postgres/target modules/docs/target modules/postgres-circe/target modules/h2/target modules/hikari/target modules/munit/target modules/h2-circe/target modules/core/target modules/example/target modules/specs2/target modules/free/target modules/bench/target project/target + run: tar cf targets.tar modules/weaver/target modules/scalatest/target modules/refined/target target modules/postgres/target modules/docs/target modules/log4cats/target modules/postgres-circe/target modules/h2/target modules/hikari/target modules/munit/target modules/h2-circe/target modules/core/target modules/example/target modules/specs2/target modules/free/target modules/bench/target project/target - name: Upload target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') From deea16663cadf54f0da4440c40e1bc347720e9f8 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Wed, 19 Jul 2023 19:13:13 +0200 Subject: [PATCH 4/4] Log4CatsDebuggingLogHandler --- ...LogHandler.scala => Log4CatsDebuggingLogHandler.scala} | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) rename modules/log4cats/src/main/scala/doobie/log4cats/{Log4CatsLogHandler.scala => Log4CatsDebuggingLogHandler.scala} (84%) diff --git a/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala b/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsDebuggingLogHandler.scala similarity index 84% rename from modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala rename to modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsDebuggingLogHandler.scala index 596c7f5c8..df050c754 100644 --- a/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsLogHandler.scala +++ b/modules/log4cats/src/main/scala/doobie/log4cats/Log4CatsDebuggingLogHandler.scala @@ -8,7 +8,13 @@ import doobie.LogHandler import doobie.util.log._ import org.typelevel.log4cats._ -class Log4CatsLogHandler[F[_]](logger: MessageLogger[F]) extends LogHandler[F]{ +/** + * A LogHandler that writes a default format to a log4cats MessageLogger. + * This is provided for debugging purposes and is not intended for production use, because it could log sensitive data. + * + * @group Constructors + */ +class Log4CatsDebuggingLogHandler[F[_]](logger: MessageLogger[F]) extends LogHandler[F]{ override def run(logEvent: LogEvent): F[Unit] = logEvent match { case Success(s, a, l, e1, e2) => logger.info(