Skip to content

Commit

Permalink
Merge pull request #426 from outr/level-filtering-fixes
Browse files Browse the repository at this point in the history
Began testing of level filtering
  • Loading branch information
darkfrog26 authored May 25, 2023
2 parents c961851 + c8ae209 commit 88548bc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/shared/src/main/scala/scribe/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ object Logger {
case (name, id) => name -> id2Logger(id)
}

/**
* Resets the global state of Scribe
*/
def reset(): Unit = {
id2Logger = Map.empty
name2Id = Map.empty
resetRoot()
}

def apply(name: String): Logger = get(name) match {
case Some(logger) => logger
case None => synchronized {
Expand Down
10 changes: 10 additions & 0 deletions core/shared/src/main/scala/scribe/writer/CacheWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class CacheWriter(max: Int = CacheWriter.DefaultMax) extends Writer {
def records: List[LogRecord] = recordCache
def output: List[LogOutput] = outputCache

def consume[Return](f: List[LogRecord] => Return): Return = try {
f(records)
} finally {
clear()
}

def consumeMessages[Return](f: List[String] => Return): Return = consume { list =>
f(list.map(_.messages.map(_.logOutput.plainText).mkString(" ")))
}

def clear(): Unit = synchronized {
recordCache = Nil
outputCache = Nil
Expand Down
64 changes: 64 additions & 0 deletions core/shared/src/test/scala/specs/LoggingLevelFilteringSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package specs

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import scribe.filter._
import scribe.{Level, Logger}
import scribe.writer.CacheWriter

class LoggingLevelFilteringSpec extends AnyWordSpec with Matchers {
private lazy val errorWriter = new CacheWriter()
private lazy val traceWriter = new CacheWriter()
private lazy val debugWriter = new CacheWriter()

private val pkg1 = "specs"
private val pkg2 = "com.foo"

"Logging Level Filtering" should {
"configure the loggers" in {
Logger.reset()
Logger.root
.clearHandlers()
.withMinimumLevel(Level.Info)
.withHandler(writer = errorWriter, minimumLevel = Some(Level.Error))
.withHandler(
writer = traceWriter,
modifiers = List(
select(packageName(pkg1), packageName(pkg2))
.include(level === Level.Trace)
.excludeUnselected
)
)
.withHandler(writer = debugWriter, minimumLevel = Some(Level.Debug))
.replace()
Logger(pkg1).withMinimumLevel(Level.Trace).replace()
}
"verify an error gets logged" in {
scribe.error("Error1")
errorWriter.consumeMessages { list =>
list should be(List("Error1"))
}
traceWriter.consumeMessages { list =>
list should be(Nil)
}
debugWriter.consumeMessages { list =>
list should be(List("Error1"))
}
}
"verify a trace message gets logged" in {
scribe.trace("Trace1")
errorWriter.consumeMessages { list =>
list should be(Nil)
}
traceWriter.consumeMessages { list =>
list should be(List("Trace1"))
}
debugWriter.consumeMessages { list =>
list should be(Nil)
}
}
"reset the root logger" in {
Logger.root.reset()
}
}
}

0 comments on commit 88548bc

Please sign in to comment.