Skip to content

Commit 582ddfe

Browse files
committed
add example of how to use log4cats module
1 parent 096cc1b commit 582ddfe

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%date | log_level=%-5level '%msg'%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="debug">
9+
<appender-ref ref="STDOUT" />
10+
</root>
11+
</configuration>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package test
2+
3+
import cats.*
4+
import cats.effect.*
5+
import cats.tagless.*
6+
import cats.tagless.aop.*
7+
import com.dwolla.util.tagless.logging.*
8+
import org.typelevel.log4cats.slf4j.Slf4jFactory
9+
10+
trait MyAlgebra[F[_]] {
11+
def foo(foo: Int, bar: String): F[Boolean]
12+
}
13+
14+
object MyAlgebra {
15+
implicit val showInt: Show[Int] = Show.fromToString
16+
implicit val showString: Show[String] = Show.show[String](identity)
17+
implicit val showBoolean: Show[Boolean] = Show.fromToString
18+
19+
implicit val loggingMyAlgebraAspect: Aspect[MyAlgebra, Show, Show] = Derive.aspect
20+
}
21+
22+
object MyApp extends IOApp.Simple {
23+
private val fakeMyAlgebra: MyAlgebra[IO] = new MyAlgebra[IO] {
24+
override def foo(foo: Int, bar: String): IO[Boolean] =
25+
IO.pure(true)
26+
}
27+
28+
override def run: IO[Unit] =
29+
Slf4jFactory.create[IO].create.flatMap { implicit logger =>
30+
fakeMyAlgebra
31+
.withMethodLogging
32+
.foo(42, "The Answer to the Ultimate Question of Life, the Universe, and Everything")
33+
.flatMap(IO.println)
34+
}
35+
}

project/AsyncUtilsBuildPlugin.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ object AsyncUtilsBuildPlugin extends AutoPlugin {
228228
},
229229
tlVersionIntroduced := Map("2.12" -> "1.2.0", "2.13" -> "1.2.0"),
230230
)
231-
.jvmPlatform(Scala2Versions)
231+
.jvmPlatform(Scala2Versions, Seq(
232+
libraryDependencies ++= Seq(
233+
"org.typelevel" %%% "log4cats-slf4j" % "2.6.0" % Test,
234+
"ch.qos.logback" % "logback-classic" % "1.4.5" % Test,
235+
),
236+
))
232237
.jsPlatform(Scala2Versions)
233238

234239
private lazy val `scalafix-rules` =

0 commit comments

Comments
 (0)