1
def buildLog(appName: String) = {
  val file = s"${sys.env("HOME")}/builds/logs/${appName}.log"
  val f = new File(file)
  Enumerator.fromFile(f).map(new String(_))
}

我想在网页上显示一个滚动日志文件,我使用Enumerator.fromFile()方法并将字节数组映射到字符串,但是我得到了一些奇怪的字符,比如

From xxx:yyy/zzz
 * branch            master     -> FETCH_HEAD
Already up-to-date.
[0m[[0minfo[0m] [0mLoading project definition from /home/zzz/builds/sources/ops-ui/project[0m
[0m[[0minfo[0m] [0mSet current project to zzz (in build file:/home/zzz/builds/sources/ops-ui/)[0m
[0m[[32msuccess[0m] [0mTotal time: 0 s, completed 2013-12-30 17:38:53[0m

如何正确显示内容?

4

2 回答 2

1

这些字符定义了控制台应该用来显示文本的颜色(例如,青色的信息和红色的错误)。

要从文件中删除字符,您可以更改日志记录配置,例如在conf/logger.xml%coloredLevel中替换为。%level

或者,您可以实现自定义LogManager以根据需要重写日志。这是受到类似emojiLogs的启发,但我没有对其进行测试

logManager ~= { lm =>
  new LogManager {
    def apply(
        data: sbt.Settings[Scope],
        state: State,
        task: Def.ScopedKey[_],
        writer: java.io.PrintWriter
    ) = {
      val l = lm.apply(data, state, task, writer)
      new Logger {
        val Escapes = Set("M\\\\[2K", "\\[0m", "\\[32m")
        def ommitColorChars(str: String) = Escapes.fold(str)(_.replaceAll(_))
        def log(level: Level.Value, message: => String) =
          l.log(level, ommitColorChars(message))
        def success(message: => String) = l.success(message)
        def trace(t: => Throwable) = l.trace(t)
        override def ansiCodesSupported = l.ansiCodesSupported
      }
    }
  }
}
于 2013-12-30T11:13:31.327 回答
0

只是为了完成更多的答案,我找到了一种更通用的删除彩色原木的方法。可以将着色字符删除为(假设您有一个带有彩色日志的字符串):

string.replaceAll("""\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]""", "")

受 Linux 命令 ( 1 ) 中等效的“sed”的启发:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
于 2014-07-15T12:01:59.783 回答