File tree Expand file tree Collapse file tree 6 files changed +162
-0
lines changed
scala/com/baeldung/scala/zio/logging
test/scala/com/baeldung/scala/zio/logging Expand file tree Collapse file tree 6 files changed +162
-0
lines changed Original file line number Diff line number Diff line change @@ -680,6 +680,13 @@ lazy val zio2 = (project in file("zio-2"))
680680 libraryDependencies += " dev.zio" %% " zio-json" % " 0.7.3" ,
681681 libraryDependencies += " dev.zio" %% " zio-test" % zioVersion % Test ,
682682 libraryDependencies += " dev.zio" %% " zio-test-sbt" % zioVersion % Test ,
683+ libraryDependencies += " dev.zio" %% " zio-logging" % " 2.1.10" ,
684+ libraryDependencies += " dev.zio" %% " zio-logging-slf4j2" % " 2.1.10" ,
685+ libraryDependencies += " ch.qos.logback" % " logback-classic" % " 1.5.6" ,
686+ // used in the article, but can't work with zio-logging-slf4j2 dependency
687+ // libraryDependencies += "dev.zio" %% "zio-logging-slf4j2-bridge" % "2.1.10",
688+ libraryDependencies += " dev.zio" %% " zio-metrics-connectors" % " 2.1.0" ,
689+ libraryDependencies += " dev.zio" %% " zio-metrics-connectors-prometheus" % " 2.1.0" ,
683690 libraryDependencies ++= scalaTestDeps,
684691 testFrameworks += new TestFramework (" zio.test.sbt.ZTestFramework" )
685692 )
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+
3+ <configuration >
4+ <appender name =" STDOUT" class =" ch.qos.logback.core.ConsoleAppender" >
5+ <encoder >
6+ <pattern >%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern >
7+ </encoder >
8+ </appender >
9+ <root level =" info" >
10+ <appender-ref ref =" STDOUT" />
11+ </root >
12+ </configuration >
Original file line number Diff line number Diff line change 1+ package com .baeldung .scala .zio .logging
2+
3+ import zio .*
4+ import zio .logging .backend .SLF4J
5+ import zio .logging .{
6+ LogColor ,
7+ LogFilter ,
8+ LogFormat ,
9+ LoggerNameExtractor ,
10+ console ,
11+ fileJson
12+ }
13+ import zio .logging .LogFormat .*
14+
15+ import java .nio .file .Path
16+ import java .time .format .DateTimeFormatter
17+
18+ object BaeldungLogger {
19+
20+ private val filter =
21+ LogFilter .logLevelByName(
22+ LogLevel .Trace ,
23+ (" com.baeldung.scala.zio.logging" , LogLevel .Info )
24+ )
25+
26+ private val logFormat : LogFormat = timestamp(
27+ DateTimeFormatter .ofPattern(" yyyy-MM-dd HH:mm:ssAZ" )
28+ ).highlight(_ => LogColor .BLUE )
29+ |-| bracketStart |-| LogFormat .loggerName(
30+ LoggerNameExtractor .trace
31+ ) |-| text(" :" ) |-| traceLine |-| bracketEnd |-|
32+ fiberId |-| level.highlight |-| label(" message" , quoted(line)).highlight
33+
34+ val consoleLogger : ULayer [Unit ] =
35+ Runtime .removeDefaultLoggers >>> console(logFormat, filter)
36+
37+ val fileLogger : ULayer [Unit ] = Runtime .removeDefaultLoggers >>> fileJson(
38+ Path .of(" baeldung-zio-logging.log" ),
39+ logFormat,
40+ LogLevel .Debug
41+ )
42+
43+ val slf4jLogger : ULayer [Unit ] = Runtime .removeDefaultLoggers >>> SLF4J .slf4j
44+
45+ // val slf4jBridgeLogger: ULayer[Unit] = Runtime.removeDefaultLoggers >>> Slf4jBridge.initialize
46+
47+ val combinedLogger : ULayer [Unit ] = consoleLogger ++ fileLogger
48+
49+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scala .zio .logging
2+
3+ import zio .*
4+ import zio .logging .*
5+ import zio .metrics .connectors .MetricsConfig
6+ import zio .metrics .connectors .prometheus .{
7+ PrometheusPublisher ,
8+ prometheusLayer ,
9+ publisherLayer
10+ }
11+
12+ import java .io .IOException
13+
14+ object BaeldungMetricsLogger {
15+
16+ private val prometheusConnector
17+ : ZLayer [Unit , IOException , PrometheusPublisher ] = (ZLayer .succeed(
18+ MetricsConfig (10 .millis)
19+ ) ++ publisherLayer) >+> prometheusLayer
20+ val metricsLogger : ZLayer [Unit , IOException , PrometheusPublisher ] =
21+ logMetrics ++ logMetricsWith(
22+ " custom_log_counter" ,
23+ " log_level"
24+ ) ++ prometheusConnector
25+
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scala .zio .logging
2+
3+ import zio .*
4+ import zio .metrics .connectors .prometheus .PrometheusPublisher
5+
6+ object BaeldungZIOLoggingApp extends ZIOAppDefault {
7+
8+ val app : ZIO [PrometheusPublisher , Any , Unit ] = for {
9+ - <- ZIO .logTrace(" This is a trace message" )
10+ _ <- ZIO .logDebug(" This is a debug message" )
11+ _ <- ZIO .logInfo(" This is an info message" )
12+ _ <- ZIO .logWarning(" This is a warning message" )
13+ _ <- ZIO .logError(" This is an error message" )
14+ _ <- ZIO .sleep(500 .millis)
15+ metricValues <- ZIO .serviceWithZIO[PrometheusPublisher ](_.get)
16+ _ <- Console .printLine(metricValues)
17+ } yield ()
18+
19+ override def run : ZIO [ZIOAppArgs & Scope , Any , Any ] =
20+ app.provideLayer(
21+ BaeldungLogger .slf4jLogger >>> BaeldungMetricsLogger .metricsLogger
22+ )
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scala .zio .logging
2+
3+ import zio .{Chunk , LogLevel , ZIO }
4+ import zio .test .Assertion .*
5+ import zio .test .*
6+ import zio .*
7+
8+ object BaeldungZIOLoggingUnitTest extends ZIOSpecDefault {
9+
10+ override def spec : Spec [TestEnvironment , Any ] =
11+ suite(" BaeldungZIOLoggingSpec" )(
12+ test(" log trace level" ) {
13+ for {
14+ - <- ZIO .logTrace(" This is a trace message" )
15+ _ <- ZIO .logDebug(" This is a debug message" )
16+ _ <- ZIO .logInfo(" This is an info message" )
17+ _ <- ZIO .logWarning(" This is a warning message" )
18+ _ <- ZIO .logError(" This is an error message" )
19+ loggerOutput <- ZTestLogger .logOutput
20+ } yield assertTrue(loggerOutput.size == 5 ) &&
21+ assert(loggerOutput.map(_.logLevel))(
22+ equalTo(
23+ Chunk (
24+ LogLevel .Trace ,
25+ LogLevel .Debug ,
26+ LogLevel .Info ,
27+ LogLevel .Warning ,
28+ LogLevel .Error
29+ )
30+ )
31+ ) &&
32+ assert(loggerOutput.map(_.message()))(
33+ equalTo(
34+ Chunk (
35+ " This is a trace message" ,
36+ " This is a debug message" ,
37+ " This is an info message" ,
38+ " This is a warning message" ,
39+ " This is an error message"
40+ )
41+ )
42+ )
43+ }
44+ ).provideLayer(Runtime .removeDefaultLoggers >>> ZTestLogger .default)
45+ }
You can’t perform that action at this time.
0 commit comments