Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect console output of command to a file?

Tags:

sbt

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?

like image 683
Alexey Romanov Avatar asked Aug 06 '14 08:08

Alexey Romanov


People also ask

How do you redirect output of a command?

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.

How do you redirect output and error of command CMD to a file?

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.

Which command is used to redirect the output to a new file?

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.


1 Answers

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)
}
like image 159
Jacek Laskowski Avatar answered Sep 20 '22 16:09

Jacek Laskowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!