From 8f44cebe2b754d35e84cc11e6d2e98e114df5e0a Mon Sep 17 00:00:00 2001 From: Bruno Unna Date: Tue, 4 Oct 2022 19:45:19 +0100 Subject: [PATCH 1/4] Add dependencies for log4j --- build.sbt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 6fc0b9814..ed6d2edcd 100644 --- a/build.sbt +++ b/build.sbt @@ -274,7 +274,11 @@ lazy val scala_libraries_3 = (project in file("scala-libraries-3")) "com.github.pureconfig" %% "pureconfig-enumeratum" % "0.17.1" ), libraryDependencies += "org.scalamock" %% "scalamock" % "5.1.0" % Test, - libraryDependencies += "com.softwaremill.retry" %% "retry" % "0.3.5" + libraryDependencies += "com.softwaremill.retry" %% "retry" % "0.3.5", + libraryDependencies ++= Seq( + "org.apache.logging.log4j" % "log4j-api-scala_2.13" % "12.0", + "org.apache.logging.log4j" % "log4j-core" % "2.19.0" % Runtime + ) ) lazy val scala_strings = (project in file("scala-strings")) From e0013f761d6237ffbe46593cf07cc3c79e34af41 Mon Sep 17 00:00:00 2001 From: Bruno Unna Date: Wed, 5 Oct 2022 19:19:50 +0100 Subject: [PATCH 2/4] Add dependencies for log4j --- build.sbt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.sbt b/build.sbt index ed6d2edcd..11e69cd8b 100644 --- a/build.sbt +++ b/build.sbt @@ -276,8 +276,8 @@ lazy val scala_libraries_3 = (project in file("scala-libraries-3")) libraryDependencies += "org.scalamock" %% "scalamock" % "5.1.0" % Test, libraryDependencies += "com.softwaremill.retry" %% "retry" % "0.3.5", libraryDependencies ++= Seq( - "org.apache.logging.log4j" % "log4j-api-scala_2.13" % "12.0", - "org.apache.logging.log4j" % "log4j-core" % "2.19.0" % Runtime + "org.apache.logging.log4j" %% "log4j-api-scala" % "12.0", + "org.apache.logging.log4j" % "log4j-core" % "2.13.0" % Runtime ) ) From c28c323499b40a28e74f23311aca5bdd5d4ba1d9 Mon Sep 17 00:00:00 2001 From: Bruno Unna Date: Wed, 5 Oct 2022 19:20:24 +0100 Subject: [PATCH 3/4] Add configuration for log4j --- scala-libraries-3/src/main/resources/log4j2.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 scala-libraries-3/src/main/resources/log4j2.xml diff --git a/scala-libraries-3/src/main/resources/log4j2.xml b/scala-libraries-3/src/main/resources/log4j2.xml new file mode 100644 index 000000000..35a6a3ccd --- /dev/null +++ b/scala-libraries-3/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file From ac9575ad88441d4bf88da227a8ffa3a0ba31fff8 Mon Sep 17 00:00:00 2001 From: Bruno Unna Date: Wed, 5 Oct 2022 19:21:10 +0100 Subject: [PATCH 4/4] Exemplify how to log with log4j --- .../com/baeldung/scala/log4j/LoggingApp.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 scala-libraries-3/src/main/scala/com/baeldung/scala/log4j/LoggingApp.scala diff --git a/scala-libraries-3/src/main/scala/com/baeldung/scala/log4j/LoggingApp.scala b/scala-libraries-3/src/main/scala/com/baeldung/scala/log4j/LoggingApp.scala new file mode 100644 index 000000000..db2fe8c03 --- /dev/null +++ b/scala-libraries-3/src/main/scala/com/baeldung/scala/log4j/LoggingApp.scala @@ -0,0 +1,14 @@ +package com.baeldung.scala.log4j + +import org.apache.logging.log4j.scala.Logging + +import scala.util.{Failure, Success, Try} + +object LoggingApp extends App with Logging { + logger.info("Writing an informative message to the log") + logger.debug("Writing a debug message to the log") + Try(1 / 0) match { + case Success(value) => logger.warn("Math has changed") + case Failure(exception) => logger.catching(exception) + } +}