开发者

Redirect Error Stream to File and Console in Windows

I want to redirect error stream from ja开发者_如何学Gova console application to file and console. In normal situation the error is displayed only in console. I want to be displayed in console and file. How can I achieve this? When I write:

java -classpath lib.jar com.hertz.test.Blad 2>error.log

Then the errors are redirecting to file but I don't see them on console. Also does anybody know how to add date and time to the logs in this situation?

I'm working in Windows 2003 Server.


This is of course a simple exercise in piping the output through a filter, in this case the tee command, which is done in Microsoft's command interpreter much the same as in JP Software's TCC/LE and (non-C-shell family) Unix shells:

java -classpath lib.jar com.hertz.test.Blad 2>&1 | tee error-and-output.log

Treating standard output and standard error differently is little more than an exercise in redirection syntax, for which this example here is but one of several possibilities, and is a separate question.

java -classpath lib.jar com.hertz.test.Blad 2>&1 1>con | tee error.log

All that remains is to obtain a tee command. There are several possibilities:

  • Use a port of a Unix tee command. There are several choices. Oft-mentioned are GNUWin32, cygwin, and unxutils. Less well known, but in some ways better, are the tools in the SFUA utility toolkit, which run in the Subsystem for UNIX-based Applications that comes right there in the box with Windows 7 Ultimate edition and Windows Server 2008 R2. (For Windows XP and Windows Server 2003, one can download and install Services for UNIX version 3.5.) This toolkit has a large number of command-line TUI tools, from mv and du, through the Korn and C shells, to perl and awk. It comes in both x86-64 and IA64 flavours as well as x86-32. The programs run in Windows' native proper POSIX environment, rather than with emulator DLLs (such as cygwin1.dll) layering things over Win32. And yes, the toolkit has tee, as well as some 300 others.
  • Use one of the many native Win32 tee commands that people have written and published. One such is Ritchie Lawrence's MTEE, which as you can see has /D and /T options to add time and date stamps to each line that it processes.
  • Use a replacement command interpreter that comes with a built-in TEE command. JP Software's TCC/LE is one such. TCC/LE has a built in TEE command. As you can see, it also has /D and /T options to add time and date stamps to each line that it processes.

As an aside: It's better for your application to add date and time stamps itself than for them to be post-processed by the TEE command. For several reasons, relating both to how applications behave when their standard streams are pipes and to how pipes work, each line of output will not necessarily be processed by TEE at the time that your application generated it in the first place. The leeway will affect both the relative (to one another) and the absolute (to the wall clock) accuracy of the timestamps that you see.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜