From 24d2c13dcbed2997af442f9a712db27dd95738d7 Mon Sep 17 00:00:00 2001 From: "shani.elh" Date: Thu, 3 Sep 2015 09:21:26 +0300 Subject: [PATCH] Added Scala Logging Support --- build.sbt | 1 + .../scala/atmos/dsl/ScalaLoggingSupport.scala | 30 +++++++++++ .../atmos/dsl/ScalaLoggingSupportSpec.scala | 50 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/main/scala/atmos/dsl/ScalaLoggingSupport.scala create mode 100644 src/test/scala/atmos/dsl/ScalaLoggingSupportSpec.scala diff --git a/build.sbt b/build.sbt index 76f0e5a..c35ba54 100644 --- a/build.sbt +++ b/build.sbt @@ -28,6 +28,7 @@ libraryDependencies ++= Seq( "io.zman" %% "rummage" % "1.3", "com.typesafe.akka" %% "akka-actor" % "2.3.2" % "provided", "org.slf4j" % "slf4j-api" % "1.7.5" % "provided", + "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0" % "provided", "org.scalatest" %% "scalatest" % "2.2.4" % "test", "org.scalamock" %% "scalamock-scalatest-support" % "3.2" % "test" ) diff --git a/src/main/scala/atmos/dsl/ScalaLoggingSupport.scala b/src/main/scala/atmos/dsl/ScalaLoggingSupport.scala new file mode 100644 index 0000000..adcdaba --- /dev/null +++ b/src/main/scala/atmos/dsl/ScalaLoggingSupport.scala @@ -0,0 +1,30 @@ +package atmos.dsl + +import atmos.monitor +import com.typesafe.scalalogging.Logger + +/** + * Separate namespace for optional Scala Logging support. + */ +object ScalaLoggingSupport { + + /** + * Creates a new event monitor that submits events to a Scala logger. + * + * @param logger The Scala logger to supply with event messages. + */ + implicit def scalaLoggerToEventMonitor(logger: Logger): monitor.LogEventsWithSlf4j = + monitor.LogEventsWithSlf4j(logger.underlying) + + /** + * Creates a new event monitor extension interface for a Scala logger. + * + * @param logger The logger to create a new event monitor extension interface for. + */ + implicit def scalaLoggerToEventMonitorExtensions(logger: Logger): LogEventsWithSlf4jExtensions = + new LogEventsWithSlf4jExtensions(logger) + + +} + + diff --git a/src/test/scala/atmos/dsl/ScalaLoggingSupportSpec.scala b/src/test/scala/atmos/dsl/ScalaLoggingSupportSpec.scala new file mode 100644 index 0000000..4e66f5c --- /dev/null +++ b/src/test/scala/atmos/dsl/ScalaLoggingSupportSpec.scala @@ -0,0 +1,50 @@ +/* ScalaLoggingSupportSpec.scala + * + * Copyright (c) 2013-2014 linkedin.com + * Copyright (c) 2013-2014 zman.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package atmos.dsl + +import atmos.dsl.ScalaLoggingSupport._ +import atmos.dsl.Slf4jSupport.{Slf4jEventLogLevels, _} +import atmos.monitor.LogEventsWithSlf4j + +import com.typesafe.scalalogging.{Logger => ScalaLogger} + +import org.scalamock.scalatest.MockFactory +import org.scalatest._ +import org.slf4j.Logger + +/** + * Test suite for [[atmos.dsl.Slf4jSupport]]. + */ +class ScalaLoggingSupportSpec extends FlatSpec with Matchers with MockFactory { + + "ScalaLoggingSupport" should "support viewing Scala-logging-compatible objects as event monitors" in { + val logger = mock[Logger] + val monitor = ScalaLogger(logger): LogEventsWithSlf4j + monitor shouldBe LogEventsWithSlf4j(logger) + (monitor: LogEventsWithSlf4jExtensions) shouldBe LogEventsWithSlf4jExtensions(monitor) + (logger: LogEventsWithSlf4jExtensions) shouldBe LogEventsWithSlf4jExtensions(LogEventsWithSlf4j(logger)) + } + + it should "return Slf4j log levels in response to generic level queries" in { + Slf4jEventLogLevels.errorLevel shouldBe LogEventsWithSlf4j.Slf4jLevel.Error + Slf4jEventLogLevels.warningLevel shouldBe LogEventsWithSlf4j.Slf4jLevel.Warn + Slf4jEventLogLevels.infoLevel shouldBe LogEventsWithSlf4j.Slf4jLevel.Info + Slf4jEventLogLevels.debugLevel shouldBe LogEventsWithSlf4j.Slf4jLevel.Debug + } + +} \ No newline at end of file