Skip to content

Commit

Permalink
Added convenience logger for lazy string evaluation (Resolves #645) a…
Browse files Browse the repository at this point in the history
…nd additional support for () => String for varargs LogFeature.
  • Loading branch information
darkfrog26 committed Oct 25, 2024
1 parent 45b0d32 commit 6cd1a2a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/shared/src/main/scala/scribe/LogFeature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait LogFeature {
object LogFeature {
def apply(f: LogRecord => LogRecord): LogFeature = (record: LogRecord) => f(record)

implicit def stringFunc2LoggableMessage(f: () => String): LogFeature = LoggableMessage.string2LoggableMessage(f())
implicit def string2LoggableMessage(s: => String): LogFeature = LoggableMessage.string2LoggableMessage(s)
implicit def logOutput2LoggableMessage(lo: => LogOutput): LogFeature = LoggableMessage[LogOutput](identity)(lo)
implicit def throwable2LoggableMessage(throwable: => Throwable): LogFeature = TraceLoggableMessage(throwable)
Expand Down
36 changes: 36 additions & 0 deletions core/shared/src/main/scala/scribe/LoggerSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,72 @@ trait LoggerSupport[F] extends Any {
log(record)
}

def trace(message: => String)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Trace, mdc, message)

def trace(features: LogFeature*)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Trace, mdc, features: _*)

def debug(message: => String)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Debug, mdc, message)

def debug(features: LogFeature*)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Debug, mdc, features: _*)

def info(message: => String)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Info, mdc, message)

def info(features: LogFeature*)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Info, mdc, features: _*)

def warn(message: => String)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Warn, mdc, message)

def warn(features: LogFeature*)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Warn, mdc, features: _*)

def error(message: => String)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Error, mdc, message)

def error(features: LogFeature*)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Error, mdc, features: _*)

def fatal(message: => String)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
line: sourcecode.Line,
mdc: MDC): F = log(Level.Fatal, mdc, message)

def fatal(features: LogFeature*)(implicit pkg: sourcecode.Pkg,
fileName: sourcecode.FileName,
name: sourcecode.Name,
Expand Down
24 changes: 23 additions & 1 deletion core/shared/src/test/scala/specs/LoggingLevelFilteringSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package specs
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import scribe.filter._
import scribe.{Level, Logger}
import scribe.{Level, LogFeature, Logger}
import scribe.writer.CacheWriter

class LoggingLevelFilteringSpec extends AnyWordSpec with Matchers {
Expand Down Expand Up @@ -57,6 +57,28 @@ class LoggingLevelFilteringSpec extends AnyWordSpec with Matchers {
list should be(Nil)
}
}
"configure the logger for only errors" in {
Logger.reset()
Logger.root
.clearHandlers()
.withHandler(writer = errorWriter, minimumLevel = Some(Level.Error))
.replace()
}
"verify an info message doesn't get evaluated" in {
scribe.info {
throw new RuntimeException("Should not evaluate!")
"testing"
}
errorWriter.consumeMessages { list =>
list should be(Nil)
}
traceWriter.consumeMessages { list =>
list should be(Nil)
}
debugWriter.consumeMessages { list =>
list should be(Nil)
}
}
"reset the root logger" in {
Logger.root.reset()
}
Expand Down

0 comments on commit 6cd1a2a

Please sign in to comment.