Skip to content

Commit

Permalink
airspec (feature): Support setting log level with -l (loglevel) (#3338)
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial authored Jan 13, 2024
1 parent 18cb3af commit 83983b9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private[airspec] class AirSpecLogger() extends AnsiColorPalette {
private val airSpecLogger = {
// Use a different spec logger for each AirSpecRunner
val l = Logger(f"wvlet.airspec.runner.AirSpecLogger_${hashCode()}%x")
// Airspec logger needs to report at least INFO level log regardless of the global log level
l.setLogLevel(wvlet.log.LogLevel.INFO)
l.setFormatter(BareFormatter)
l
}
Expand Down
34 changes: 24 additions & 10 deletions airspec/src/main/scala/wvlet/airspec/runner/AirSpecSbtRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package wvlet.airspec.runner

import sbt.testing.{Task, TaskDef}
import wvlet.airspec.runner.AirSpecSbtRunner.AirSpecConfig
import wvlet.log.{LogSupport, Logger}
import wvlet.log.{LogSupport, Logger, LogLevel}

import scala.util.matching.Regex

Expand All @@ -39,6 +39,7 @@ private[airspec] class AirSpecSbtRunner(config: AirSpecConfig, val remoteArgs: A
// use airframe's code before sbt detaches the class loader
taskLogger.clearHandlers
Logger.clearAllHandlers
Logger.setDefaultLogLevel(LogLevel.INFO)
""
}

Expand All @@ -55,18 +56,31 @@ private[airspec] class AirSpecSbtRunner(config: AirSpecConfig, val remoteArgs: A
private[airspec] object AirSpecSbtRunner extends LogSupport {
def newRunner(args: Array[String], remoteArgs: Array[String], testClassLoader: ClassLoader): AirSpecSbtRunner = {

// Set log levels given in the command line args: -L(package)=(log level)
args.filter(_.startsWith("-L")).foreach { l =>
l.stripPrefix("-L").split("=") match {
case Array(pkg, level) =>
val logLevel = wvlet.log.LogLevel(level)
wvlet.log.Logger(pkg).setLogLevel(logLevel)
case _ =>
warn(s"Ignoring invalid argument: ${l}. Use -L(package)=(log level) to set log levels")
// Set log level with -l (log level)
val remaining = Array.newBuilder[String]
var i = 0
var logLevel: LogLevel = Logger.getDefaultLogLevel
while (i < args.length) {
args(i) match {
case "-l" if i < args.length - 1 =>
val logLevel = LogLevel(args(i + 1))
Logger.setDefaultLogLevel(logLevel)
i += 1
case arg if arg.startsWith("-L") =>
arg.stripPrefix("-L").split("=") match {
case Array(pkg, level) =>
val logLevel = LogLevel(level)
Logger(pkg).setLogLevel(logLevel)
case _ =>
warn(s"Ignoring invalid argument: ${arg}. Use -L(package)=(log level) to set log levels")
}
case other =>
remaining += other
}
i += 1
}

new AirSpecSbtRunner(AirSpecConfig(args), remoteArgs, testClassLoader)
new AirSpecSbtRunner(AirSpecConfig(remaining.result()), remoteArgs, testClassLoader)
}

case class AirSpecConfig(args: Array[String]) {
Expand Down
9 changes: 8 additions & 1 deletion docs/airspec.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ $ sbt
> testOnly -- (pattern)/(pattern) # Run nested tests matching the nested pattern (/ is a dlimiter)

# Configure log levels of airframe-log
> testOnly -- -l (level) # Set the default log level
> testOnly -- -L(package):(level) # Set log level for a package

# sbt's default test functionalities:
Expand All @@ -192,7 +193,13 @@ Test names will be checked as case-insensitive partial match, so you only need t

### Configure Log Levels

AirSpec natively supports [airframe-log](https://wvlet.org/airframe/docs/airframe-log.html) for logging. To temporally change the log level of your test classes, use `-L` option:
AirSpec natively supports [airframe-log](https://wvlet.org/airframe/docs/airframe-log.html) for logging. To temporally change the log level, use `-l (log level)` option:

```scala
> testOnly -- -l debug
```

To change the log level only for a specific package or a class, use `-L(package or class)=(log level)` option:

```scala
> testOnly -- -Lorg.mydomain.myapp:debug
Expand Down

0 comments on commit 83983b9

Please sign in to comment.