diff --git a/README.md b/README.md index 30ca0b0..6a275d7 100755 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ In addition ScalaLogging offers the trait `Logging` which conveniently provides Prerequisites ------------- -* Scala 2.10.0 -* SLF4J 1.7.2 or Log4j 2.0-beta3 +* Scala 2.10.1 +* SLF4J 1.7.5 or Log4j 2.0-beta4 Using ScalaLogging ------------------ @@ -44,6 +44,15 @@ The following example shows how to add a dependency to the latest **snapshot** v libraryDependencies += "com.typesafe" %% "scalalogging-log4j" % "1.1.0-SNAPSHOT" +Example +------- + +Small example of usage is located in sub-project `scalalogging-log4j-test`. See `Log4jLogExample.scala`. +To run example from sbt: type `project scalalogging-log4j-test` and then type `run` command. + +Second example is located in `project scalalogging-slf4j-test`. +The example is configured to run the combination of slf4j with native jdk logger. + Contribution policy ------------------- diff --git a/project/Build.scala b/project/Build.scala index a92a488..b4f3731 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -1,7 +1,9 @@ import sbt._ import sbt.Keys._ import com.typesafe.sbt.SbtScalariform._ +import sbt.ScalaVersion import sbtrelease.ReleasePlugin._ +import scala.Some object Build extends Build { @@ -37,6 +39,7 @@ object Build extends Build { "scalalogging-slf4j-test", file("scalalogging-slf4j-test"), settings = commonSettings ++ Seq( + libraryDependencies ++= Seq(Dependencies.Test.slf4jJdk14), publishArtifact := false ), dependencies = Seq(scalaloggingSlf4j) @@ -58,13 +61,14 @@ object Build extends Build { "scalalogging-log4j-test", file("scalalogging-log4j-test"), settings = commonSettings ++ Seq( + libraryDependencies ++= Seq(Dependencies.Test.log4jCore), publishArtifact := false ), dependencies = Seq(scalaloggingLog4j) - ) + ) def commonSettings = - Defaults.defaultSettings ++ + Defaults.defaultSettings ++ scalariformSettings ++ releaseSettings ++ Seq( @@ -113,6 +117,8 @@ object Build extends Build { val specs2 = "org.specs2" %% "specs2" % "1.14" % "test" val mockito = "org.mockito" % "mockito-all" % "1.9.0" % "test" val hamcrest = "org.hamcrest" % "hamcrest-all" % "1.1" % "test" + val log4jCore = "org.apache.logging.log4j" % "log4j-core" % "2.0-beta4" + val slf4jJdk14 = "org.slf4j" % "slf4j-jdk14" % "1.7.5" } } } diff --git a/scalalogging-log4j-test/src/main/resources/log4j2.xml b/scalalogging-log4j-test/src/main/resources/log4j2.xml new file mode 100644 index 0000000..22f4185 --- /dev/null +++ b/scalalogging-log4j-test/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/scalalogging-log4j-test/src/main/scala/com/typesafe/scalalogging/log4j/Log4jLogExample.scala b/scalalogging-log4j-test/src/main/scala/com/typesafe/scalalogging/log4j/Log4jLogExample.scala new file mode 100644 index 0000000..121c350 --- /dev/null +++ b/scalalogging-log4j-test/src/main/scala/com/typesafe/scalalogging/log4j/Log4jLogExample.scala @@ -0,0 +1,66 @@ +/* + * Copyright 2012 Copyright 2012 Typesafe Inc. + * + * 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 com.typesafe.scalalogging.log4j + +/** + * Example + */ +case class LogExample(name: String) extends Logging { + + lazy val greetings = { + logger.info("greetings value retrieved!") + s"Hey ${name}, welcome to the world of optimized logging." + } + + private def method1() { + val v = 3 + // log value + logger.info(s"method1 => value: v=${v}") + } + + private def method2() { + // this log doesn't appear, because severity in log4j.xml is INFO + logger.debug("method2 => " + greetings) + } + + private def method3() { + logger.info("method3-begin") + // before this log, is called log defined in body of val greetings. + logger.info("method3 => " + greetings) + logger.info("method3-end") + } + + private def method4() { + // because greetings is lazy val and was accessed in method3. + // there is only one log message there + logger.info("method4 => " + greetings) + } + + def all() = { + method1 + method2 + method3 + method4 + } +} + +/** + * Example main + */ +object Log4jLogExample extends App { + LogExample("You") all +} + diff --git a/scalalogging-log4j-test/src/test/scala/com/typesafe/scalalogging/slf4j/Log4jLoggerSpec.scala b/scalalogging-log4j-test/src/test/scala/com/typesafe/scalalogging/log4j/Log4jLoggerSpec.scala similarity index 100% rename from scalalogging-log4j-test/src/test/scala/com/typesafe/scalalogging/slf4j/Log4jLoggerSpec.scala rename to scalalogging-log4j-test/src/test/scala/com/typesafe/scalalogging/log4j/Log4jLoggerSpec.scala diff --git a/scalalogging-slf4j-test/src/main/scala/com/typesafe/scalalogging/slf4j/Slf4jLogExample.scala b/scalalogging-slf4j-test/src/main/scala/com/typesafe/scalalogging/slf4j/Slf4jLogExample.scala new file mode 100644 index 0000000..57499a1 --- /dev/null +++ b/scalalogging-slf4j-test/src/main/scala/com/typesafe/scalalogging/slf4j/Slf4jLogExample.scala @@ -0,0 +1,52 @@ +/* + * Copyright 2012 Copyright 2012 Typesafe Inc. + * + * 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 com.typesafe.scalalogging.slf4j + +/** + * Example + */ +case class LogExample(name: String) extends Logging { + + private def method0() { + // default severity is INFO (see: ~jre/lib/logging.properties), so this message is not logged. + logger.debug("method0 => a debug message") + } + + private def method1() { + logger.info(s"method1 => Hey ${name}. How are you.") + } + + private def method2() { + logger.warn("method2 => a warning message") + } + + def all() = { + method0 + method1 + method2 + } +} + +/** + * Example main + * If you want change configuration of jdk logger, + * run program with the option -Djava.util.logging.config.file={your-file-path}. + * + */ +object Slf4jLogExample extends App { + LogExample("You") all +} +