Last active
May 25, 2024 10:20
-
-
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/3a6f176469404c30d94e365c131c8e62b662bdb3
This file contains 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
// 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.4.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