Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand All @@ -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
-------------------

Expand Down
10 changes: 8 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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"
}
}
}
13 changes: 13 additions & 0 deletions scalalogging-log4j-test/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</loggers>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2012 Copyright 2012 Typesafe Inc. <http://www.typesafe.com>
*
* 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
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012 Copyright 2012 Typesafe Inc. <http://www.typesafe.com>
*
* 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
}