Last active
February 3, 2026 20:27
-
-
Save dacr/a56189f7ec1cdcc975ec35a6c18dea0e to your computer and use it in GitHub Desktop.
ZIO learning - using zio logging great features with log4j2 / published by https://github.com/dacr/code-examples-manager #fc148a1e-4e3a-461c-b4fe-24e8a2cf4c0d/54953af6c4ecbd6d6325dfd734b921d3d6aebb09
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
| // summary : ZIO learning - using zio logging great features with log4j2 | |
| // keywords : scala, zio, learning, logging, pure-functional, log4j2, mdc, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : fc148a1e-4e3a-461c-b4fe-24e8a2cf4c0d | |
| // created-on : 2023-05-09T07:33:28+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 "org.apache.logging.log4j:log4j-api:2.20.0" | |
| //> using dep "org.apache.logging.log4j:log4j-core:2.20.0" | |
| //> using dep "org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0" | |
| // --------------------- | |
| import zio.* | |
| import zio.logging.* | |
| import ZIOAspect.* | |
| import zio.logging.{LogFormat, removeDefaultLoggers} | |
| import zio.logging.backend.{SLF4J} | |
| // ===================================================================================================================== | |
| object LogBackHelpers { | |
| import org.slf4j.{Logger, LoggerFactory} | |
| import org.apache.logging.log4j.{LogManager, Level} | |
| import org.apache.logging.log4j.core.config.* | |
| def configureLogBack(xmlConfig: String): Unit = { | |
| val configStream = new java.io.ByteArrayInputStream(xmlConfig.getBytes) | |
| val source = new ConfigurationSource(configStream); | |
| Configurator.initialize(null, source); | |
| } | |
| } | |
| val logConfig = | |
| """<?xml version="1.0" encoding="UTF-8"?> | |
| |<Configuration> | |
| | <Appenders> | |
| | <Console name="ConsoleOut"> | |
| | <PatternLayout> | |
| | <!--<pattern>%highlight{%m - %MDC%n}{STYLE=Logback}</pattern>--> | |
| | <pattern>%highlight{%d{ISO8601}{GMT+0}Z [%t] %level %logger{18} func:%M - L:%L - %m - %MDC%n}{STYLE=Logback}</pattern> | |
| | <!-- <pattern>%highlight{%d{ISO8601}{GMT+0}Z [%t] %level %logger{18} func:%M - L:%L - %m - %MAP%n}{STYLE=Logback}</pattern> --> | |
| | </PatternLayout> | |
| | </Console> | |
| | </Appenders> | |
| | <Loggers> | |
| | <Logger name="com.example" level="info"/> | |
| | <Root level="info"> | |
| | <AppenderRef ref="ConsoleOut"/> | |
| | </Root> | |
| | </Loggers> | |
| |</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