Dependency inject logger and replace println

This commit is contained in:
Guntis Smaukstelis
2023-05-21 22:37:33 +03:00
parent 00dd4a4f4a
commit 760ef4e719
10 changed files with 97 additions and 59 deletions
+7 -4
View File
@@ -4,6 +4,7 @@ import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.implicits.toTraverseOps
import db.DBService
import org.typelevel.log4cats.slf4j.Slf4jLogger
import java.time.LocalDateTime
@@ -12,8 +13,10 @@ object Main {
val from = LocalDateTime.of(2023, 4, 28, 10, 0)
val to = LocalDateTime.of(2023, 4, 28, 13, 30)
for {
// fetchResultEither <- FetchService.fetchFromDate(LocalDate.of(2023, 4, 28)).attempt
fetchResultEither <- FetchService.fetchInRange(from, to).attempt
log <- Slf4jLogger.create[IO]
fetch <- FetchService.of
// fetchResultEither <- fetch.fetchFromDate(LocalDate.of(2023, 4, 28)).attempt
fetchResultEither <- fetch.fetchInRange(from, to).attempt
fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
fetchResult = fetchResultEither.getOrElse(List.empty)
(fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
@@ -22,8 +25,8 @@ object Main {
(saveErrors, successSaves) = saveResults.partitionMap(identity)
successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
errors = fetchServiceError ++ fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
_ <- IO.println(s"errors: $errors")
_ <- IO.println(s"successes: $successes")
_ <- log.info(s"errors: $errors")
_ <- log.info(s"successes: $successes")
} yield (successes, errors)
}