Why does the ">" command in a Windows console not redirect all messages to a file?

3815
Cherry

I am trying to build a Scala project with sbt, so I run a command:

sbt clean test > log.log 

Which means that any messages that the sbt tool writes to the Windows console should be written to the "log.log" file. But sometimes I get stacktrace written to the console and not into the file:

C:\path>sbt clean test > log.log java.lang.ExceptionInInitializerError at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) Caused by: java.lang.ClassCastException: Class org.infinispan.configuration.parsing.Parser60 does not implement org.infinispan.configuration.parsing.ConfigurationParser 

Why does ">" command not redirect all messages to a file?

21

1 ответ на вопрос

35
rAlen

What you have pasted is not command standard output (STDOUT), but error output of the command (STDERR).

When you add "> output_file" to command, you are only redirecting STDOUT to that file, not STDERR.

If you want to output errors, to the same file as standard output you need to use

sbt clean test > log.log 2>&1 

what "2>&1" does, is, it says to output error to same place as standard output results.

You can also do something like this:

sbt clean test > log.log 2>error.log 

It will output STDOUT to log.log, and STDERR to second file called error.log, if you want to separate them.

See this about command redirector operators

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

В чем разница между `2> & 1` из вашего ответа и` 1 <& 2` из ссылки? Я всегда видел это по-своему, и имеет смысл и другой способ (просто перенаправить «ввод» вместо «вывод», но в остальном выглядит одинаково), но интересно увидеть второй вариант. Joe 9 лет назад 3
семантика ... `2> & 1` говорит, что вывод из STDERR должен быть перенаправлен на тот же вывод, что и STDOUT. `1 <& 2` говорит, что вывод из STDERR должен использоваться как вход для STDOUT. Оба дают одинаковый результат и являются просто вопросом предпочтения. SeanC 9 лет назад 6
Обратите внимание, что вы должны поставить `2> & 1` ** после **`> log.log`. smwikipedia 5 лет назад 0