score:22
I think it's because SBT runs the tests in parallel and some initialization code in Slf4j is not thread safe (!). See http://jira.qos.ch/browse/SLF4J-167 ... it's been reported more than 2 years ago!
As a workaround I initialize Slf4j by loading the root logger before the tests run. To do so just add this to your SBT settings:
testOptions += Setup( cl =>
cl.loadClass("org.slf4j.LoggerFactory").
getMethod("getLogger",cl.loadClass("java.lang.String")).
invoke(null,"ROOT")
)
score:0
slf4s 1.0.7 depends on slf4j 1.6.1 as you can see [here][1]. Try to use this version instead of 1.6.3 for your other slf4j dependencies.
score:1
I had the same issue. Ended up just instantiating an empty logger in the class definition.
If I applied my method to your code then it would be,
import com.weiglewilczek.slf4s.{Logger, Logging}
class MyClass with Logging {
val _ = Logger("") // <--Solved problem
def doSomething() {
logger.debug("Hello world")
}
}
Note that I am very new to scala so I don't know the full implications of what I have just done.
score:1
The problem is that while the first thread is initializing the underlying logging implementation (blocking), for all other concurrent threads, SubstituteLoggerFactory is created. This substitute logger factory returns a SubstituteLogger instead the actual logger implementation. This issue isn't resolved in SLF4J-167.
It is less likely to meet this issue in Java, because often logger objects are created as a static variable, so the LoggerFactory is being initialized during the class loading. In Scala there is no static modifier and companion objects are initialized lazily. Furthermore, most testing frameworks in Scala execute tests in parallel.
To workaround this issue, you can change the test environment: as Bruno Bieth suggested you can initialize the LoggerFactory before the tests start. You can do that also in the test code rather than the build settings. You can also set the test to run sequentially, but then you lose speed.
Alternatively you can eagerly initialize a Logger initialized in a companion object. Ugly, but in most cases ensures that Foo objects created concurrently won't be initialized with a SubstituteLogger.
class Foo {
val logger = Foo.singletonLogger
}
object Foo {
val singletonLogger = LoggerFactory.getLogger(getClass)
}
Source: stackoverflow.com
Related Query
- How to get logging working in scala unit tests with testng, slf4s, and logback
- How to get Scala imports working in IntelliJ IDEA with the Play framework?
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- Configuring Apache Spark Logging with Scala and logback
- How to get ScalaTest correctly reporting tests results when using scalacheck with Propspec and PropertyCheck?
- How can I get random data generated for scala case classes with the ability to "change some values" for unit testing?
- How can I get Scala Named and Default arguments to work with macros
- How to get Scala case class fields and values as (String, String) with Shapeless or Macro
- How to get IntelliJ IDEA 14 working with Scala on Android (SBT)
- scala TreeMap - how to get indexOf and element to use with slice or view method
- How to get sbteclipse working with Scala 2.9
- Writing Unit Tests of file readers with Scala and SBT
- How to run Scala tests with the TestNG plugin in Scala IDE / Eclipse?
- how to get websocket working with play and Concurrent.broadcast
- How can I get complete stacktraces for exceptions thrown in tests when using sbt and testng?
- How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?
- How to use IntelliJ with Play Framework and Scala
- How do I disambiguate in Scala between methods with vararg and without
- How to create annotations and get them in scala
- How to get all request parameters in Play and Scala
- how to get started with Elastic Search using scala client
- How to stub a method call with an implicit matcher in Mockito and Scala
- Scala / Lift: How do I write unit tests that test a snippet's response to different parameters
- How to apply manually evolutions in tests with Slick and Play! 2.4
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- Working with Some() and Option() in Scala
- How to know if a Scala file modified with IntelliJ Idea is saved and if it is checked into CVS?
- How To Create Temporary Directory in Scala Unit Tests
- How to fold a Scala iterator and get a lazily evaluated sequence as result?
- How do you run cucumber with Scala 2.11 and sbt 0.13?
More Query from same tag
- Succinct way of reading data from file into an immutable 2 dimensional array in Scala
- Single column delimited string rdd to correctly columned dataframe
- Jdbc data type to Spark SQL datatype
- Unresolved dependency in SBT: no ivy file found
- Play 2.3.8 - Unable to start Kamon 0.4.0
- What's the simplest definition of a MyList.flatten
- Scala type checking compilation error
- Spark : ClassCastException while converting string into date
- Cost of Builder#result for immutable collections?
- Gatling : Update content of JSON file before using it as request body
- How to synchronize Intellij and sbt builds for a scala project
- playframework view engine, is it only groovy based still?
- Why does partition parameter of SparkContext.textFile not take effect?
- search sequence of sequences for single first match efficiently
- Implementing a typeclass using type parameters versus abstract types
- Why can't I iterate over (Int, Int, Int) with Map?
- Implicits over type inference for object transformation
- Mocking a Kafka consumer in Scala
- Importing existing sbt project to IntelliJ
- Importing in Scala - How to add a jar to the classpath permanently
- Spark error: missing parameter type in map()
- ScalaCheck's generators with ScalaTest's behavior functions
- Inline if statement that Intellij understands
- Link promise to another
- Iterate over imported scala classes
- How to create custom list accumulator, i.e. List[(Int, Int)]?
- update json values with json4s for a json path
- scala - how to concatenate columns of a DataFrame with concat_ws?
- Refreshing SBT project in Intellij Idea switches Java to 7
- Spark notebook worksheets not saved with docker