Skip to content

Commit

Permalink
add example of how to use log4cats module
Browse files Browse the repository at this point in the history
  • Loading branch information
bpholt committed Jun 6, 2024
1 parent 096cc1b commit 582ddfe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
11 changes: 11 additions & 0 deletions log4cats/src/test/resources/logback-test.xml
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>
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)
}
}
7 changes: 6 additions & 1 deletion project/AsyncUtilsBuildPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ object AsyncUtilsBuildPlugin extends AutoPlugin {
},
tlVersionIntroduced := Map("2.12" -> "1.2.0", "2.13" -> "1.2.0"),
)
.jvmPlatform(Scala2Versions)
.jvmPlatform(Scala2Versions, Seq(
libraryDependencies ++= Seq(
"org.typelevel" %%% "log4cats-slf4j" % "2.6.0" % Test,
"ch.qos.logback" % "logback-classic" % "1.4.5" % Test,
),
))
.jsPlatform(Scala2Versions)

private lazy val `scalafix-rules` =
Expand Down

0 comments on commit 582ddfe

Please sign in to comment.