diff --git a/internal/util-logging/src/main/scala/sbt/internal/util/ManagedLogger.scala b/internal/util-logging/src/main/scala/sbt/internal/util/ManagedLogger.scala index d4cef3fe0..ae5f73939 100644 --- a/internal/util-logging/src/main/scala/sbt/internal/util/ManagedLogger.scala +++ b/internal/util-logging/src/main/scala/sbt/internal/util/ManagedLogger.scala @@ -1,9 +1,10 @@ package sbt.internal.util -import sbt.util._ +import sbt.util.{ Level, _ } import org.apache.logging.log4j.{ Logger => XLogger } import org.apache.logging.log4j.message.ObjectMessage import sjsonnew.JsonFormat + import scala.reflect.runtime.universe.TypeTag import sbt.internal.util.codec.JsonProtocol._ @@ -19,10 +20,12 @@ class ManagedLogger( override def trace(t: => Throwable): Unit = logEvent(Level.Error, TraceEvent("Error", t, channelName, execId)) override def log(level: Level.Value, message: => String): Unit = { - xlogger.log( - ConsoleAppender.toXLevel(level), - new ObjectMessage(StringEvent(level.toString, message, channelName, execId)) - ) + if (shouldLog(level)) { + xlogger.log( + ConsoleAppender.toXLevel(level), + new ObjectMessage(StringEvent(level.toString, message, channelName, execId)) + ) + } } // send special event for success since it's not a real log level @@ -38,18 +41,24 @@ class ManagedLogger( final def infoEvent[A: JsonFormat: TypeTag](event: => A): Unit = logEvent(Level.Info, event) final def warnEvent[A: JsonFormat: TypeTag](event: => A): Unit = logEvent(Level.Warn, event) final def errorEvent[A: JsonFormat: TypeTag](event: => A): Unit = logEvent(Level.Error, event) + def logEvent[A: JsonFormat: TypeTag](level: Level.Value, event: => A): Unit = { - val v: A = event - val tag = StringTypeTag[A] - LogExchange.getOrElseUpdateJsonCodec(tag.key, implicitly[JsonFormat[A]]) - // println("logEvent " + tag.key) - val entry: ObjectEvent[A] = ObjectEvent(level, v, channelName, execId, tag.key) - xlogger.log( - ConsoleAppender.toXLevel(level), - new ObjectMessage(entry) - ) + if (shouldLog(level)) { + val v: A = event + val tag = StringTypeTag[A] + LogExchange.getOrElseUpdateJsonCodec(tag.key, implicitly[JsonFormat[A]]) + // println("logEvent " + tag.key) + val entry: ObjectEvent[A] = ObjectEvent(level, v, channelName, execId, tag.key) + xlogger.log( + ConsoleAppender.toXLevel(level), + new ObjectMessage(entry) + ) + } } + private def shouldLog(level: Level.Value) = + xlogger.getLevel.isMoreSpecificThan(ConsoleAppender.toXLevel(level)) + @deprecated("No longer used.", "1.0.0") override def ansiCodesSupported = ConsoleAppender.formatEnabledInEnv } diff --git a/internal/util-logging/src/main/scala/sbt/util/AbtractLogger.scala b/internal/util-logging/src/main/scala/sbt/util/AbtractLogger.scala index 253238038..29226878a 100644 --- a/internal/util-logging/src/main/scala/sbt/util/AbtractLogger.scala +++ b/internal/util-logging/src/main/scala/sbt/util/AbtractLogger.scala @@ -17,9 +17,9 @@ abstract class AbstractLogger extends Logger { /** Defined in terms of other methods in Logger and should not be called from them. */ final def log(event: LogEvent): Unit = { event match { - case s: Success => success(s.msg) - case l: Log => log(l.level, l.msg) - case t: Trace => trace(t.exception) + case s: Success => if (successEnabled) success(s.msg) + case l: Log => if (atLevel(l.level)) log(l.level, l.msg) + case t: Trace => if (traceEnabled) trace(t.exception) case setL: SetLevel => setLevel(setL.newLevel) case setT: SetTrace => setTrace(setT.level) case setS: SetSuccess => setSuccessEnabled(setS.enabled)