Logging

Weaver only outputs the logs for tests that failed - the logs are neatly collected alongside the failure information and reported after all the tests have run.

Additionally, each log can have a context associated with it - which gets printed alongside the message.

import weaver._
import cats.effect._

object MySuite extends SimpleIOSuite {

  val randomUUID = IO(java.util.UUID.randomUUID())

  loggedTest("logging for success") { log =>
    for {
      x <- randomUUID
      y <- randomUUID
      _ <- log.info(s"Generated $x and $y")
    } yield expect(x != y)
  }

}

object MyAnotherSuite extends SimpleIOSuite {
  import scala.util.Random.alphanumeric

  val randomString = IO(alphanumeric.take(10).mkString(""))

  loggedTest("failure should print logs") { log =>
    for {
      currentTime <- IO.realTime.map(_.toSeconds)
      context = Map("time" -> currentTime.toString, "purpose" -> "docs")
      _ <- log.info("Starting the test...", context)
      x <- randomString
      _ <- log.debug(s"Generated random string: $x")
    } yield expect(x.length > 20)
  }
}

The report would look something like this:

repl.MdocSessionMdocAppMySuite
logging for success 12ms

repl.MdocSessionMdocAppMyAnotherSuite
failure should print logs 11ms

*************FAILURES*************
repl.MdocSessionMdocAppMyAnotherSuite
failure should print logs 11ms
  assertion failed (multiple_suites_logging.md:41)

  } yield expect(x.length > 20)
                 | |      |
                 | 10     false
                 XXlrc23xgb


    [INFO]  14:45:59 [multiple_suites_logging.md:38] Starting the test...
        time    -> 1715784359
        purpose -> docs
    [DEBUG] 14:45:59 [multiple_suites_logging.md:40] Generated random string: XXlrc23xgb Total 2, Failed 1, Passed 1, Ignored 0, Cancelled 0