Last active
May 25, 2024 10:20
-
-
Save dacr/b62589f684088071ac3dfebda0e56774 to your computer and use it in GitHub Desktop.
ZIO learning - testing slf4j logging bridge / published by https://github.com/dacr/code-examples-manager #b64f070c-3b27-414c-9587-943bc80bb335/d0b778bad0a0a99a3c44eb3e780f74e07b59d411
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 - testing slf4j logging bridge | |
// keywords : scala, zio, logging, slf4j, bridge, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache2 | |
// id : b64f070c-3b27-414c-9587-943bc80bb335 | |
// created-on : 2023-09-08T16:30:50+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.16" | |
//> using dep "dev.zio::zio-logging-slf4j2-bridge:2.1.14" | |
// --------------------- | |
// initial example coming from package zio.logging.example | |
import zio.{LogAnnotation => LLogAnnotation, *} | |
import zio.logging.* | |
import java.util.UUID | |
object Slf4j2SimpleApp extends ZIOAppDefault { | |
val logFilter: LogFilter[String] = LogFilter.logLevelByName( | |
LogLevel.Info, | |
"zio.logging.slf4j" -> LogLevel.Debug, | |
"SLF4J-LOGGER" -> LogLevel.Warning | |
) | |
val logConfig = ConsoleLoggerConfig( | |
LogFormat.colored + | |
LogFormat.space + | |
LogFormat.label("name", LoggerNameExtractor.loggerNameAnnotationOrTrace.toLogFormat()) + | |
LogFormat.space + | |
LogFormat.logAnnotation(LogAnnotation.UserId) + | |
LogFormat.space + | |
LogFormat.logAnnotation(LogAnnotation.TraceId) + | |
LogFormat.space + | |
LogFormat.spans + | |
LogFormat.space + | |
LogFormat.default, | |
logFilter | |
) | |
val loggerLayer = Runtime.removeDefaultLoggers >>> consoleJsonLogger(logConfig) >+> zio.logging.slf4j.bridge.Slf4jBridge.initialize | |
// val loggerLayer = Runtime.removeDefaultLoggers >>> consoleLogger(logConfig) >+> zio.logging.slf4j.bridge.Slf4jBridge.initialize | |
override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] = loggerLayer | |
private val users = List.fill(2)(UUID.randomUUID()) | |
def legacyCode1(message: String): Int = { | |
val legacyLogger = org.slf4j.LoggerFactory.getLogger("LegacyLogger") | |
legacyLogger.info(s"Hello lecacy1 code message=$message") | |
42 | |
} | |
def legacyCode2(greetings: String): Int = { | |
import java.util.logging.* | |
val legacyLogger = Logger.getLogger("LegacyLogger") | |
legacyLogger.info(s"$greetings lecacy2 code") | |
42 * 42 | |
} | |
override def run: ZIO[Scope, Any, ExitCode] = | |
for { | |
_ <- ZIO.logInfo("Start") | |
traceId <- ZIO.succeed(UUID.randomUUID()) | |
_ <- ZIO.foreachPar(users) { uId => | |
ZIO.logSpan("some-processing") { | |
ZIO.logInfo("Starting user operation") *> | |
// ZIO.logInfo("Confidential user operation") @@ SLF4J.logMarkerName("CONFIDENTIAL") *> | |
ZIO.logSpan("legacyCode1") { ZIO.attempt(legacyCode1("Hello")) } | |
@@ ZIOAspect.logged *> | |
// ZIO.attempt(legacyCode2("Good morning")) *> | |
ZIO.sleep(500.millis) *> | |
ZIO.logInfo("Stopping user operation") | |
} @@ LogAnnotation.UserId(uId.toString) | |
} @@ LogAnnotation.TraceId(traceId) @@ zio.logging.loggerName("zio.logging.example.UserOperation") | |
_ <- ZIO.logInfo("Done") | |
} yield ExitCode.success | |
} | |
Slf4j2SimpleApp.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment