mirror of https://github.com/sbt/sbt.git
Filter out color in CosoleAppender only
Fixes sbt/sbt#3348 Ref #101 The new logger, based on log4j separates the concern of the log producer (Logger) and the handlers that takes actions (Appender, e.g for displaying on Console). As such filtering of color should be performed only in the ConsoleAppender.
This commit is contained in:
parent
f6370063f4
commit
d796084ff4
|
|
@ -1,6 +1,5 @@
|
|||
package sbt.internal.util
|
||||
|
||||
import scala.compat.Platform.EOL
|
||||
import sbt.util._
|
||||
import java.io.{ PrintStream, PrintWriter }
|
||||
import java.util.Locale
|
||||
|
|
@ -269,11 +268,16 @@ class ConsoleAppender private[ConsoleAppender] (
|
|||
useFormat: Boolean,
|
||||
suppressedMessage: SuppressedTraceContext => Option[String]
|
||||
) extends AbstractAppender(name, null, LogExchange.dummyLayout, true) {
|
||||
import scala.Console.{ BLUE, GREEN, RED, RESET, YELLOW }
|
||||
import scala.Console.{ BLUE, GREEN, RED, YELLOW }
|
||||
|
||||
private final val SUCCESS_LABEL_COLOR = GREEN
|
||||
private final val SUCCESS_MESSAGE_COLOR = RESET
|
||||
private final val NO_COLOR = RESET
|
||||
private val reset: String = {
|
||||
if (ansiCodesSupported && useFormat) scala.Console.RESET
|
||||
else ""
|
||||
}
|
||||
|
||||
private val SUCCESS_LABEL_COLOR = GREEN
|
||||
private val SUCCESS_MESSAGE_COLOR = reset
|
||||
private val NO_COLOR = reset
|
||||
|
||||
override def append(event: XLogEvent): Unit = {
|
||||
val level = ConsoleAppender.toLevel(event.getLevel)
|
||||
|
|
@ -333,7 +337,7 @@ class ConsoleAppender private[ConsoleAppender] (
|
|||
* @return The formatted message.
|
||||
*/
|
||||
private def formatted(format: String, msg: String): String =
|
||||
s"${RESET}${format}${msg}${RESET}"
|
||||
s"$reset${format}${msg}$reset"
|
||||
|
||||
/**
|
||||
* Select the right color for the label given `level`.
|
||||
|
|
@ -362,14 +366,14 @@ class ConsoleAppender private[ConsoleAppender] (
|
|||
private def appendLog(labelColor: String, label: String, messageColor: String, message: String): Unit =
|
||||
out.lockObject.synchronized {
|
||||
message.lines.foreach { line =>
|
||||
val labeledLine = s"$RESET[${formatted(labelColor, label)}] ${formatted(messageColor, line)}"
|
||||
val labeledLine = s"$reset[${formatted(labelColor, label)}] ${formatted(messageColor, line)}"
|
||||
write(labeledLine)
|
||||
}
|
||||
}
|
||||
|
||||
private def write(msg: String): Unit = {
|
||||
val cleanedMsg =
|
||||
if (!useFormat) EscHelpers.removeEscapeSequences(msg)
|
||||
if (!useFormat || !ansiCodesSupported) EscHelpers.removeEscapeSequences(msg)
|
||||
else msg
|
||||
out.println(cleanedMsg)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ import sbt.util._
|
|||
class MultiLogger(delegates: List[AbstractLogger]) extends BasicLogger {
|
||||
@deprecated("No longer used.", "1.0.0")
|
||||
override lazy val ansiCodesSupported = delegates exists supported
|
||||
|
||||
private[this] lazy val allSupportCodes = delegates forall supported
|
||||
private[this] def supported = (_: AbstractLogger).ansiCodesSupported
|
||||
|
||||
override def setLevel(newLevel: Level.Value): Unit = {
|
||||
|
|
@ -33,22 +31,8 @@ class MultiLogger(delegates: List[AbstractLogger]) extends BasicLogger {
|
|||
def logAll(events: Seq[LogEvent]): Unit = delegates.foreach(_.logAll(events))
|
||||
def control(event: ControlEvent.Value, message: => String): Unit = delegates.foreach(_.control(event, message))
|
||||
private[this] def dispatch(event: LogEvent): Unit = {
|
||||
val plainEvent = if (allSupportCodes) event else removeEscapes(event)
|
||||
for (d <- delegates)
|
||||
if (d.ansiCodesSupported)
|
||||
d.log(event)
|
||||
else
|
||||
d.log(plainEvent)
|
||||
}
|
||||
|
||||
private[this] def removeEscapes(event: LogEvent): LogEvent =
|
||||
{
|
||||
import EscHelpers.{ removeEscapeSequences => rm }
|
||||
event match {
|
||||
case s: Success => new Success(rm(s.msg))
|
||||
case l: Log => new Log(l.level, rm(l.msg))
|
||||
case ce: ControlEvent => new ControlEvent(ce.event, rm(ce.msg))
|
||||
case _: Trace | _: SetLevel | _: SetTrace | _: SetSuccess => event
|
||||
}
|
||||
for (d <- delegates) {
|
||||
d.log(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue