Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get logging working in scala unit tests with testng, slf4s, and logback
    primarykey
    data
    text
    <p>I'm new to Scala, and not that familiar with recent developments in Java, so I am having what I assume is a basic problem.</p> <p>I'm writing some Scala code, and testing it with test fixtures using ScalaTest and TestNG. The code under test uses slf4s to perform its logging, backed by logback.</p> <p>In my 'build.sbt' file I have dependencies for all the libraries I need:</p> <pre><code>scalaVersion := "2.9.1" // Add test dependencies on scalatest and testng libraryDependencies ++= Seq("org.scalatest" %% "scalatest" % "1.6.1" % "test", "org.testng" % "testng" % "6.1.1" % "test") // Use the slf4j logging facade for logging libraryDependencies += "org.slf4j" % "slf4j-api" % "1.6.3" //use the slf4j connectors to implement the JCL logging facade in terms of slf4j (which in turn is implemented in terms of logback) //confused yet? libraryDependencies += "org.slf4j" % "jcl-over-slf4j" % "1.6.3" //use logback for the back-end slf4j logging impl. libraryDependencies ++= Seq("ch.qos.logback" % "logback-core" % "0.9.30", "ch.qos.logback" % "logback-classic" % "0.9.30") //use slf4s to expose the slf4j logging facade in scala libraryDependencies += "com.weiglewilczek.slf4s" %% "slf4s" % "1.0.7" //Add the dispatch HTTP client dependency libraryDependencies ++= Seq( "net.databinder" %% "dispatch-http" % "0.8.5" ) //I can't figure out how to use the dispatch HTTP client library, so just use the apache one libraryDependencies += "org.apache.httpcomponents" % "httpclient" % "4.1.2" </code></pre> <p>I perform logging like this (code simplified for readability):</p> <pre><code>class MyClass extends Logging { def doSomething() { logger.debug("Hello world") } } </code></pre> <p>when I run a test that exercises this code (using the 'sbt test' command) I do not see the debug message, but I do see this printed to the console:</p> <pre><code> SLF4J: The following loggers will not work because they were created SLF4J: during the default configuration phase of the underlying logging system. SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger SLF4J: MyClass </code></pre> <p>I have a logback.xml file in src/test/resources, and I know logging itself is working as I see output from the Apache HttpClient library (which uses JCL).</p> <p>Am I missing something? The information I'm logging is helpful in exploring the behavior of my code with tests, and besides it seems like this should work. I have of course read the page at <a href="http://www.slf4j.org/codes.html#substituteLogger" rel="noreferrer">http://www.slf4j.org/codes.html#substituteLogger</a> but I don't see how my logger is getting created before the logging subsystem has been configured.</p> <p><em>UPDATE</em>: Here is the contents of my logback.xml:</p> <pre><code>&lt;configuration&gt; &lt;appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"&gt; &lt;!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --&gt; &lt;encoder&gt; &lt;pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %line --- %msg%n&lt;/pattern&gt; &lt;/encoder&gt; &lt;/appender&gt; &lt;root level="debug"&gt; &lt;appender-ref ref="STDOUT" /&gt; &lt;/root&gt; &lt;/configuration&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload