-
Notifications
You must be signed in to change notification settings - Fork 3
Add async-utils-log4cats module #184
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
Open
bpholt
wants to merge
3
commits into
main
Choose a base branch
from
logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
log4cats/src/main/scala/com/dwolla/util/tagless/logging/LoggingWeaveMaterializer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.dwolla.util.tagless.logging | ||
|
|
||
| import cats.* | ||
| import cats.effect.syntax.all.* | ||
| import cats.effect.{Trace as _, *} | ||
| import cats.syntax.all.* | ||
| import cats.tagless.aop.* | ||
| import cats.tagless.syntax.all.* | ||
| import org.typelevel.log4cats.Logger | ||
|
|
||
| final class LoggingWeaveMaterializerOps[Alg[_[_]], F[_]](val alg: Alg[F]) extends AnyVal { | ||
| def withMethodLogging(implicit | ||
| aspect: Aspect[Alg, Show, Show], | ||
| logger: Logger[F], | ||
| F: MonadCancelThrow[F], | ||
| ): Alg[F] = | ||
| alg.weave.mapK(new LoggingWeaveMaterializer[F]) | ||
| } | ||
|
|
||
| final class LoggingWeaveMaterializer[F[_] : MonadCancelThrow : Logger] extends (Aspect.Weave[F, Show, Show, *] ~> F) { | ||
| override def apply[A](fa: Aspect.Weave[F, Show, Show, A]): F[A] = { | ||
| val methodArguments = fa.domain.map { l => | ||
| l.map { a => | ||
| val value = a.target match { | ||
| case Now(v) => a.instance.show(v) | ||
| case _ => "unevaluated lazy value" | ||
| } | ||
|
|
||
| s"${a.name}=$value" | ||
| }.mkString(", ") | ||
| }.mkString_("(", ", ", ")") | ||
|
|
||
| fa.codomain.target.guaranteeCase { | ||
| case Outcome.Succeeded(out) => | ||
| out.flatMap { a => | ||
| Logger[F].info(s"${fa.algebraName}.${fa.codomain.name}$methodArguments returning ${fa.codomain.instance.show(a)}") | ||
| } | ||
| case Outcome.Errored(ex) => | ||
| Logger[F].warn(ex)(s"${fa.algebraName}.${fa.codomain.name}$methodArguments raised an exception") | ||
| case Outcome.Canceled() => | ||
| Logger[F].warn(s"${fa.algebraName}.${fa.codomain.name}$methodArguments was canceled") | ||
| } | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
log4cats/src/main/scala/com/dwolla/util/tagless/logging/package.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.dwolla.util.tagless | ||
|
|
||
| package object logging { | ||
| implicit def toLoggingWeaveMaterializerOps[Alg[_[_]], F[_]](alg: Alg[F]): LoggingWeaveMaterializerOps[Alg, F] = | ||
| new LoggingWeaveMaterializerOps(alg) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <configuration> | ||
| <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
| <encoder> | ||
| <pattern>%date | log_level=%-5level '%msg'%n</pattern> | ||
| </encoder> | ||
| </appender> | ||
|
|
||
| <root level="debug"> | ||
| <appender-ref ref="STDOUT" /> | ||
| </root> | ||
| </configuration> |
55 changes: 55 additions & 0 deletions
55
log4cats/src/test/scala/com/dwolla/util/tagless/logging/LoggingWeaveMaterializerSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.dwolla.util.tagless.logging | ||
|
|
||
| import cats.Show | ||
| import cats.effect.IO | ||
| import cats.tagless.Derive | ||
| import cats.tagless.aop.Aspect | ||
| import munit.{CatsEffectSuite, ScalaCheckEffectSuite} | ||
| import org.scalacheck.effect.PropF | ||
| import org.typelevel.log4cats.testing.TestingLogger | ||
|
|
||
| class LoggingWeaveMaterializerSpec | ||
| extends CatsEffectSuite | ||
| with ScalaCheckEffectSuite { | ||
|
|
||
| test("instrumented logging") { | ||
| PropF.forAllF { (foo: Int, bar: String, returnValue: Either[Throwable, Boolean]) => | ||
| implicit val testLogger: TestingLogger[IO] = TestingLogger.impl[IO]() | ||
|
|
||
| val target = new TestWeaveTarget[IO] { | ||
| override def foo(foo: Int, bar: String): IO[Boolean] = | ||
| IO.fromEither(returnValue) | ||
| } | ||
|
|
||
| target | ||
| .withMethodLogging | ||
| .foo(foo, bar) | ||
| .attempt | ||
| .product(testLogger.logged) | ||
| .map { | ||
| case (Right(out), logged) => | ||
| assertEquals(Right(out), returnValue) | ||
| assertEquals(logged, Vector(TestingLogger.INFO( | ||
| message = s"TestWeaveTarget.foo(foo=$foo, bar=$bar) returning $out", | ||
| throwOpt = None | ||
| ))) | ||
| case (Left(out), logged) => | ||
| assertEquals(Left(out), returnValue) | ||
| assertEquals(logged, Vector(TestingLogger.WARN( | ||
| message = s"TestWeaveTarget.foo(foo=$foo, bar=$bar) raised an exception", | ||
| throwOpt = Option(out) | ||
| ))) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| trait TestWeaveTarget[F[_]] { | ||
| def foo(foo: Int, bar: String): F[Boolean] | ||
| } | ||
|
|
||
| object TestWeaveTarget { | ||
| implicit def show[A]: Show[A] = Show.fromToString | ||
|
|
||
| implicit val loggingTestWeaveTargetAspect: Aspect[TestWeaveTarget, Show, Show] = Derive.aspect | ||
| } |
35 changes: 35 additions & 0 deletions
35
log4cats/src/test/scalajvm/com/dwolla/util/tagless/logging/Example.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package test | ||
|
|
||
| import cats.* | ||
| import cats.effect.* | ||
| import cats.tagless.* | ||
| import cats.tagless.aop.* | ||
| import com.dwolla.util.tagless.logging.* | ||
| import org.typelevel.log4cats.slf4j.Slf4jFactory | ||
|
|
||
| trait MyAlgebra[F[_]] { | ||
| def foo(foo: Int, bar: String): F[Boolean] | ||
| } | ||
|
|
||
| object MyAlgebra { | ||
| implicit val showInt: Show[Int] = Show.fromToString | ||
| implicit val showString: Show[String] = Show.show[String](identity) | ||
| implicit val showBoolean: Show[Boolean] = Show.fromToString | ||
|
|
||
| implicit val loggingMyAlgebraAspect: Aspect[MyAlgebra, Show, Show] = Derive.aspect | ||
| } | ||
|
|
||
| object MyApp extends IOApp.Simple { | ||
| private val fakeMyAlgebra: MyAlgebra[IO] = new MyAlgebra[IO] { | ||
| override def foo(foo: Int, bar: String): IO[Boolean] = | ||
| IO.pure(true) | ||
| } | ||
|
|
||
| override def run: IO[Unit] = | ||
| Slf4jFactory.create[IO].create.flatMap { implicit logger => | ||
| fakeMyAlgebra | ||
| .withMethodLogging | ||
| .foo(42, "The Answer to the Ultimate Question of Life, the Universe, and Everything") | ||
| .flatMap(IO.println) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this should be pretty easily testable; we should probably add tests before merging this PR.