diff --git a/core/shared/src/main/scala/scribe/LogFeature.scala b/core/shared/src/main/scala/scribe/LogFeature.scala index 40f9b545e..48806ccfb 100644 --- a/core/shared/src/main/scala/scribe/LogFeature.scala +++ b/core/shared/src/main/scala/scribe/LogFeature.scala @@ -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) diff --git a/core/shared/src/main/scala/scribe/LoggerSupport.scala b/core/shared/src/main/scala/scribe/LoggerSupport.scala index 1f22cee4e..cff9b42e8 100644 --- a/core/shared/src/main/scala/scribe/LoggerSupport.scala +++ b/core/shared/src/main/scala/scribe/LoggerSupport.scala @@ -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, diff --git a/core/shared/src/test/scala/specs/LoggingLevelFilteringSpec.scala b/core/shared/src/test/scala/specs/LoggingLevelFilteringSpec.scala index 52ae39adb..9231d2e12 100644 --- a/core/shared/src/test/scala/specs/LoggingLevelFilteringSpec.scala +++ b/core/shared/src/test/scala/specs/LoggingLevelFilteringSpec.scala @@ -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 { @@ -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() }