What are the means of redirecting output from a single command to a file in sbt?
I could exit sbt, execute sbt mycommand > out.txt, and start it again, but I'm wondering if there's an alternative?
Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input. As we saw before, the cat command concatenates files and puts them all together to the standard output.
The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.
The >> shell command is used to redirect the standard output of the command on the left and append (add) it to the end of the file on the right.
In Add a custom logger you can find more information about what's required to have a custom logger that logs to a file - use extraLoggers and add an instance of sbt.AbstractLogger that does the saving.
You may find my answer to Displaying timestamp for debug mode in SBT? useful. The example copied here:
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With