Skip to content

Commit

Permalink
Add examples/FileOutput.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
kheino authored and hejfelix committed May 20, 2022
1 parent cb532a2 commit bb48f28
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions modules/examples/src/main/scala/examples/FileOutput.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package examples

import cats.effect.{IO, IOApp, Resource}
import java.io.{PrintWriter, FileWriter}
import org.legogroup.woof.{*, given}

object FileOutput extends IOApp.Simple:

given Filter = Filter.everything
given Printer = NoColorPrinter()

val fileOutput =
def writeLine(line: String, logPath: String) =
val writer = IO(PrintWriter(FileWriter(logPath, true)))
val res = Resource.make(writer) { w => IO(w.close) }
res.use { w => IO(w.println(line)) }
new Output[IO]:
def output(str: String) = writeLine(str, "woof.log")
def outputError(str: String) = writeLine(str, "woof.err")

def run =
for
given Logger[IO] <- DefaultLogger.makeIo(fileOutput, Output.fromConsole)
_ <- Logger[IO].info("Hello, Info!")
_ <- Logger[IO].error("Hello, Error!")
yield ()

0 comments on commit bb48f28

Please sign in to comment.