Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 3, 2023 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/a9c7dcb151cebbe516f64be5d3085212 to your computer and use it in GitHub Desktop.
Save dacr/a9c7dcb151cebbe516f64be5d3085212 to your computer and use it in GitHub Desktop.
ZIO learning - using zio logging great features with logback / published by https://github.com/dacr/code-examples-manager #54e632d9-0e23-4e1c-aec8-b9a0e2e4fee7/2ad197ad21554dc115a0d396d66bff1e193818ad
// summary : ZIO learning - using zio logging great features with logback
// keywords : scala, zio, learning, logging, pure-functional, logback, mdc, @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 : 54e632d9-0e23-4e1c-aec8-b9a0e2e4fee7
// created-on : 2023-05-07T18:04:29+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "dev.zio::zio-logging:2.1.13"
//> using dep "dev.zio::zio-logging-slf4j2:2.1.13"
//> using dep "ch.qos.logback:logback-classic:1.4.7"
// ---------------------
import zio.*
import zio.logging.*
import ZIOAspect.*
import zio.logging.{LogFormat, removeDefaultLoggers}
import zio.logging.backend.SLF4J
// =====================================================================================================================
object LogBackHelpers {
import org.slf4j.LoggerFactory
import ch.qos.logback.classic
def configureLogBack(xmlConfig: String): Unit = {
val loggerContext = LoggerFactory.getILoggerFactory.asInstanceOf[classic.LoggerContext]
loggerContext.reset()
val configurator = new classic.joran.JoranConfigurator()
val configStream = new java.io.ByteArrayInputStream(xmlConfig.getBytes)
configurator.setContext(loggerContext)
configurator.doConfigure(configStream)
}
}
val logConfig =
"""<?xml version="1.0" encoding="UTF-8"?>
|
|<configuration
| scan="false"
| scanPeriod="10 seconds"
| xmlns="http://ch.qos.logback/xml/ns/logback"
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
|
| <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
| <encoder>
| <pattern>%highlight(%date %level %logger{10} [%file:%line] %msg - %kvp%n)</pattern>
| </encoder>
| </appender>
|
| <root level="info">
| <appender-ref ref="STDOUT"/>
| </root>
|</configuration>
|""".stripMargin
import LogBackHelpers._
configureLogBack(logConfig)
// =====================================================================================================================
object Encapsulated extends ZIOAppDefault {
override val bootstrap = removeDefaultLoggers ++ SLF4J.slf4j
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