Skip to content

Commit 0ca131d

Browse files
Merge pull request #553 from yadavan88/scala-logging
Scala logging
2 parents e9ee9a1 + 51b29c4 commit 0ca131d

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

build.sbt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ThisBuild / organizationName := "core-scala"
77
val jUnitInterface = "com.novocode" % "junit-interface" % "0.11" % "test"
88
val catsEffect = "org.typelevel" %% "cats-effect" % "3.4.5"
99
val scalaReflection = "org.scala-lang" % "scala-reflect" % scalaV
10-
val logback = "ch.qos.logback" % "logback-classic" % "1.2.3"
10+
val logback = "ch.qos.logback" % "logback-classic" % "1.3.5"
1111

1212
val scalaTestDeps = Seq(
1313
"org.scalatest" %% "scalatest" % "3.2.15" % Test,
@@ -156,7 +156,7 @@ lazy val scala_test = (project in file("scala-test"))
156156

157157
lazy val scala_akka_dependencies: Seq[ModuleID] = Seq(
158158
"com.typesafe.akka" %% "akka-actor-typed" % "2.6.19",
159-
logback,
159+
"ch.qos.logback" % "logback-classic" % "1.2.3",
160160
"com.typesafe.akka" %% "akka-actor-testkit-typed" % "2.6.19" % Test,
161161
"com.lightbend.akka" %% "akka-stream-alpakka-mongodb" % "2.0.1",
162162
"com.typesafe.akka" %% "akka-stream" % "2.6.19",
@@ -328,7 +328,9 @@ lazy val scala_libraries_4 = (project in file("scala-libraries-4"))
328328
scalaReflection % Provided,
329329
"org.tpolecat" %% "skunk-core" % "0.3.2",
330330
sparkSqlDep,
331-
sparkCoreDep
331+
sparkCoreDep,
332+
logback,
333+
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.5"
332334
),
333335
libraryDependencies ++= Seq(
334336
"com.clever-cloud.pulsar4s" %% "pulsar4s-core" % "2.9.0",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
<root level="trace">
8+
<appender-ref ref="STDOUT"/>
9+
</root>
10+
</configuration>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.scala.logging
2+
3+
import com.typesafe.scalalogging.{CanLog, LazyLogging, Logger, StrictLogging}
4+
import org.slf4j.LoggerFactory
5+
6+
class ScalaLoggingSample {
7+
8+
val loggerV1 = Logger("BaeldungLogger")
9+
val loggerV2 = Logger(getClass.getName)
10+
val loggerV3 = Logger(classOf[ScalaLoggingSample])
11+
val loggerV4 = Logger[ScalaLoggingSample]
12+
val loggerV5 = Logger(LoggerFactory.getLogger("FromSlf4jLogger"))
13+
14+
}
15+
case class RequestId(id: String)
16+
17+
object LoggingImplicits {
18+
implicit case object WithRequestId extends CanLog[RequestId] {
19+
override def logMessage(originalMsg: String, a: RequestId): String =
20+
s"[REQ: ${a.id}] $originalMsg"
21+
}
22+
}
23+
24+
object TestApp extends App {
25+
val sample = new ScalaLoggingSample
26+
sample.loggerV1.info("This is an info message")
27+
sample.loggerV2.error("This is an error message")
28+
sample.loggerV3.warn("This is a warning message")
29+
sample.loggerV4.debug("This is a debug message")
30+
sample.loggerV5.trace("This is a trace message")
31+
32+
sample.loggerV5.whenDebugEnabled {
33+
println("This block is executed only if logger level is DEBUG")
34+
}
35+
36+
def contextUsageMethod = {
37+
import LoggingImplicits._
38+
val ctxLogger = Logger.takingImplicit[RequestId]("ContextLogger")
39+
implicit val reqId = RequestId("user-1234")
40+
println()
41+
println()
42+
println()
43+
val message = "This is a message with user request context"
44+
ctxLogger.info(message)
45+
}
46+
47+
contextUsageMethod
48+
49+
val country = "India"
50+
val capital = "NewDelhi"
51+
sample.loggerV5.info(s"$capital is the capital of $country")
52+
val slf4jLogger = sample.loggerV5.underlying
53+
slf4jLogger.info("{} is the capital of {}", capital, country)
54+
}
55+
56+
class LazyLoggingSample extends LazyLogging {
57+
logger.info("This is from lazy logging")
58+
}
59+
class StrictLoggingSample extends StrictLogging {
60+
logger.info("This is from strict logging")
61+
}

0 commit comments

Comments
 (0)