Created
March 29, 2019 11:42
-
-
Save sshark/ad991cedbf40a26376a6affb905f158f to your computer and use it in GitHub Desktop.
Not really my thing, it is copied from https://github.com/ChristopherDavenport/log4cats and modified to compile and run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cats.effect.{ExitCode, IO, IOApp, Sync} | |
| import io.chrisdavenport.log4cats.Logger | |
| import io.chrisdavenport.log4cats.slf4j.Slf4jLogger | |
| object MyThing extends IOApp { | |
| // Arbitrary Local Function Declaration | |
| def doSomething[F[_]: Sync]: F[Unit] = { | |
| // Impure But What 90% of Folks I know do with log4s | |
| implicit def unsafeLogger[F[_]: Sync] = Slf4jLogger.getLogger[F] | |
| Logger[F].info("Logging Start Something") *> | |
| Sync[F].delay(println("I could be doing anything")).attempt.flatMap { | |
| case Left(e) => Logger[F].error(e)("Something Went Wrong") | |
| case Right(_) => Sync[F].pure(()) | |
| } | |
| } | |
| def safelyDoThings[F[_]: Sync]: F[Unit] = | |
| for { | |
| logger <- Slf4jLogger.create[F] | |
| _ <- logger.info("Logging at start of safelyDoThings") | |
| something <- Sync[F] | |
| .delay(println("I could do anything")) | |
| .onError { case e => logger.error(e)("Something Went Wrong in safelyDoThings") } | |
| _ <- logger.info("Logging at end of safelyDoThings") | |
| } yield something | |
| def passForEasierUse[F[_]: Sync: Logger] = | |
| for { | |
| _ <- Logger[F].info("Logging at start of passForEasierUse") | |
| something <- Sync[F] | |
| .delay(println("I could do anything")) | |
| .onError { case e => Logger[F].error(e)("Something Went Wrong in passForEasierUse") } | |
| _ <- Logger[F].info("Logging at end of passForEasierUse") | |
| } yield something | |
| override def run(args: List[String]): IO[ExitCode] = | |
| doSomething[IO] *> | |
| MyThing.safelyDoThings[IO] *> { | |
| implicit def unsafeLogger[F[_]: Sync] = Slf4jLogger.getLogger[F] | |
| MyThing.passForEasierUse[IO] | |
| } *> | |
| IO(ExitCode.Success) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment