Displaying timestamp for debug mode in SBT?

流过昼夜 提交于 2019-12-23 22:05:13

问题


My sbt update is very slow, I'd like to see what happens in details.

So, I have:

sbt --debug update > sbtupdate.log

The problem is that the log does not have timestamp for each line, how to enable it?


回答1:


To my knowledge, it is not possible with only SBT options. However this question provides a good solution. I will rephrase the answer for your specific case. All the following supposes a Unix OS but it should be possible to adapt it for a Windows environment (and straightforward if using Cygwin).

First, you need a script called for example predate.sh that contains:

#!/bin/bash
while read line ; do
    echo "$(date): ${line}"
done

The date command can be changed following your needs. Then, you must make this script executable with chmod u+x predate.sh.

Finally run your initial command and pipe the standard output to our new script:

sbt --debug update | ./predate.sh > sbtupdate.log

Please read the linked question, it contains interesting other responses.




回答2:


I think what you need is to develop a custom logger as described in Add a custom logger:

The setting extraLoggers can be used to add custom loggers. A custom logger should implement [AbstractLogger]. extraLoggers is a function ScopedKey[_] => Seq[AbstractLogger]. This means that it can provide different logging based on the task that requests the logger.

In the custom logger you'd prepend messages with timestamp.

def datedPrintln = (m: String) =>
  println(s"+++ ${java.util.Calendar.getInstance().getTime()} $m")

extraLoggers := {
  val clientLogger = FullLogger {
    new Logger {
      def log(level: Level.Value, message: => String): Unit =
        if(level >= Level.Info) datedPrintln(s"$message at $level")
      def success(message: => String): Unit = datedPrintln(s"success: $message")
      def trace(t: => Throwable): Unit = datedPrintln(s"trace: throwable: $t")
    }
  }
  val currentFunction = extraLoggers.value
  (key: ScopedKey[_]) => clientLogger +: currentFunction(key)
}

You can find out about how to develop your own logger in my answer to Custom extraLogger not getting [success] messages in sbt?



来源:https://stackoverflow.com/questions/22398788/displaying-timestamp-for-debug-mode-in-sbt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!