Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
Filter out log events more aggressively
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Nov 7, 2017
1 parent 9eb7803 commit a3b37de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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._

Expand All @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a3b37de

Please sign in to comment.