我是Scala的新手,对Java的最新发展也不太熟悉,所以我有一个我认为是基本问题。
我正在编写一些Scala代码,并使用ScalaTest和TestNG进行测试。被测试的代码使用slf4s进行日志记录,由logback支持。
在我的“build.sbt”文件中,我已经添加了所有需要的库的依赖项:
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"
我会像这样记录日志(为了易读性,代码进行了简化):
class MyClass extends Logging {
def doSomething() {
logger.debug("Hello world")
}
}
当我运行一个测试来执行这段代码(使用'sbt test'命令),我没有看到调试信息,但我确实在控制台上看到了这个打印:
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
我在src/test/resources中有一个logback.xml文件,并且我知道日志记录本身是有效的,因为我可以看到Apache HttpClient库(它使用JCL)的输出。
我是否遗漏了什么?我记录的信息有助于通过测试探索我的代码的行为,并且似乎应该起作用。当然,我已经阅读了http://www.slf4j.org/codes.html#substituteLogger上的页面,但我不知道我的记录器是如何在日志记录子系统被配置之前被创建的。
更新:这是我的logback.xml的内容:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %line --- %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>