Skip to content

Commit

Permalink
airspec (fix): Fixes #3523 and #3516 to properly set and reset log le…
Browse files Browse the repository at this point in the history
…vels with -l,-L options (#3528)

Fixes #3523, #3516
  • Loading branch information
xerial authored May 6, 2024
1 parent 8ef41e8 commit 7bc692e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
26 changes: 19 additions & 7 deletions airspec/src/main/scala/wvlet/airspec/runner/AirSpecSbtRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ private[airspec] object AirSpecSbtRunner extends LogSupport {
def newRunner(args: Array[String], remoteArgs: Array[String], testClassLoader: ClassLoader): AirSpecSbtRunner = {

// Set log level with -l (log level)
val remaining = Array.newBuilder[String]
var i = 0
var logLevel: LogLevel = Logger.getDefaultLogLevel
val remaining = Array.newBuilder[String]
var i = 0
var logLevel: LogLevel = Logger.getDefaultLogLevel
val additionalLogLevels = Map.newBuilder[String, LogLevel]
while (i < args.length) {
args(i) match {
case "-l" if i < args.length - 1 =>
Expand All @@ -71,8 +72,7 @@ private[airspec] object AirSpecSbtRunner extends LogSupport {
case arg if arg.startsWith("-L") =>
arg.stripPrefix("-L").split("=") match {
case Array(pkg, level) =>
val logLevel = LogLevel(level)
Logger(pkg).setLogLevel(logLevel)
additionalLogLevels += pkg -> LogLevel(level)
case _ =>
warn(s"Ignoring invalid argument: ${arg}. Use -L(package)=(log level) to set log levels")
}
Expand All @@ -82,10 +82,22 @@ private[airspec] object AirSpecSbtRunner extends LogSupport {
i += 1
}

new AirSpecSbtRunner(AirSpecConfig(remaining.result(), logLevel), remoteArgs, testClassLoader)
new AirSpecSbtRunner(
AirSpecConfig(
remaining.result(),
defaultLogLevel = logLevel,
additionalLogLevels = additionalLogLevels.result()
),
remoteArgs,
testClassLoader
)
}

case class AirSpecConfig(args: Array[String], defaultLogLevel: LogLevel = LogLevel.INFO) {
case class AirSpecConfig(
args: Array[String],
defaultLogLevel: LogLevel = LogLevel.INFO,
additionalLogLevels: Map[String, LogLevel] = Map.empty
) {
val specMatcher: AirSpecMatcher = {
// For now, we only support regex-based test name matcher using the first argument
args.find(x => !x.startsWith("-")) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import sbt.testing.*
import wvlet.airframe.{Design, Session}
import wvlet.airspec.runner.AirSpecSbtRunner.AirSpecConfig
import wvlet.airspec.spi.{AirSpecContext, AirSpecException}
import wvlet.log.{Logger, LogSupport}
import wvlet.log.{Logger, LogSupport, LogLevel}

import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.util.{Failure, Success, Try}
Expand Down Expand Up @@ -88,11 +88,19 @@ private[airspec] class AirSpecTaskRunner(
*/
def runTask: Future[Unit] = {
val startTimeNanos = System.nanoTime()
var prevLogLevel = Logger(testClassName).getLogLevel
val prevLogLevel = Map.newBuilder[String, LogLevel]
prevLogLevel += testClassName -> Logger(testClassName).getLogLevel
Future
.apply {
// Set the default log level for the class
Logger(testClassName).setLogLevel(config.defaultLogLevel)

// Set log level for other classes
config.additionalLogLevels.foreach { case (pkg, level) =>
prevLogLevel += pkg -> Logger(pkg).getLogLevel
Logger(pkg).setLogLevel(level)
}

// Start a background log level scanner thread. If a thread is already running, reuse it.
compat.startLogScanner
}
Expand All @@ -110,7 +118,11 @@ private[airspec] class AirSpecTaskRunner(
}
.transform { case ret =>
compat.stopLogScanner
Logger(testClassName).setLogLevel(prevLogLevel)

// Reset log levels
prevLogLevel.result().foreach { case (pkg, level) =>
Logger(pkg).setLogLevel(level)
}
ret
}
.recover { case e: Throwable =>
Expand Down

0 comments on commit 7bc692e

Please sign in to comment.