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

Add module for log4cats support for debugging purposes #1896

Merged
merged 4 commits into from
Jul 19, 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 8 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
13 changes: 13 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -137,6 +138,7 @@ lazy val doobie = project.in(file("."))
h2,
`h2-circe`,
hikari,
log4cats,
postgres,
`postgres-circe`,
refined,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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._

/**
* 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(
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)
}
}