https://kotlinlang.org logo
#functional
Title
# functional
d

dnowak

03/27/2020, 3:56 PM
Hi, I’m looking for advice on how to do logging in functional code. Functional -> no side effects -> no logs ;-). Our code is base on Arrow’s IO, so I would appreciate some hints for logging in this context.
r

raulraja

03/27/2020, 9:57 PM
@dnowak you can also make it poart of your effects by exposing it as a type class:
Copy code
interface Logger<F> {
  val A: Async<F>
  fun info(msg: String): Kind<F, Unit>
}

object IOLogger : Logger<ForIO> {
  override val A = IO.async()
  override fun info(msg: String): IO<Unit> = IO.effect { log(msg) }
}

//...other runtimes like Rx, etc will also be able to provide an impl for it
g

Gopal S Akshintala

03/29/2020, 7:08 AM
r

raulraja

03/29/2020, 7:36 AM
A complete library that integrates with slf4j too asn example in scala https://github.com/ChristopherDavenport/log4cats
It also observes F for polymorphism and abstracts what runtime it targets
d

dnowak

04/01/2020, 11:53 AM
Thank you for all the responses. Right now
flatTap
is all what I need.
👍 1
8 Views