Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:20
Show Gist options
  • Save dacr/cbe9c2b235c93623013eae94b4ebba01 to your computer and use it in GitHub Desktop.
Save dacr/cbe9c2b235c93623013eae94b4ebba01 to your computer and use it in GitHub Desktop.
ZIO learning - using zio logging great features / published by https://github.com/dacr/code-examples-manager #5dd422d4-9a90-4450-bd24-23a46ee351b9/d821fe352fb4400854d6185cc7e0175d8d07f47a
// summary : ZIO learning - using zio logging great features
// keywords : scala, zio, learning, logging, pure-functional, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 5dd422d4-9a90-4450-bd24-23a46ee351b9
// created-on : 2022-06-30T22:08:34+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "dev.zio::zio-logging:2.1.13"
// ---------------------
import zio.*
import zio.logging.*
import ZIOAspect.*
object Encapsulated extends ZIOAppDefault {
val doThat =
ZIO.logSpan("doThat") {
ZIO.succeed(true) @@ logged("result")
}
val getIt =
ZIO.logSpan("processThat") {
for {
x <- ZIO.succeed(42)
_ <- doThat
_ <- ZIO.logInfo(s"$x has been received")
} yield x
}
val myapp =
ZIO.logSpan("myapp") {
for {
_ <- ZIO.logInfo("Application has started")
_ <- ZIO.logWarning("Configuration is wrong")
userId <- Random.nextUUID
result <- getIt @@ annotated("userId" -> userId.toString) @@ logged("getIt result") // The result is appended :) Great !!!
_ <- ZIO.logInfo("Application has finished")
} yield ()
}
def run = myapp
}
Encapsulated.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment