如何使用testng,slf4s和logback在scala单元测试中进行日志记录

ane*_*son 19 testng scala logback slf4j

我是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"
Run Code Online (Sandbox Code Playgroud)

我执行这样的日志记录(为了便于阅读而简化了代码):

class MyClass extends Logging {
   def doSomething() {
      logger.debug("Hello world")
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行一个运行此代码的测试时(使用'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
Run Code Online (Sandbox Code Playgroud)

我在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>
Run Code Online (Sandbox Code Playgroud)

Bru*_*eth 19

我认为这是因为SBT并行运行测试,Slf4j中的一些初始化代码不是线程安全的(!).请参阅http://jira.qos.ch/browse/SLF4J-167 ...它已被报道超过2年前!

作为一种解决方法,我通过在测试运行之前加载根记录器来初始化Slf4j.为此,只需将其添加到您的SBT设置:

testOptions += Setup( cl =>
   cl.loadClass("org.slf4j.LoggerFactory").
     getMethod("getLogger",cl.loadClass("java.lang.String")).
     invoke(null,"ROOT")
)
Run Code Online (Sandbox Code Playgroud)

  • 为了避免谷歌搜索什么设置,如果你没有导入测试,请使用build.scala中的Tests.Setup._ (2认同)