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/a56189f7ec1cdcc975ec35a6c18dea0e to your computer and use it in GitHub Desktop.
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/fc5688b5183865e5a00fc40a166be6b31c9c0ecf
// 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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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.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 "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