Redirect System.out and System.err to a file or a pipe
- System.out and System.err of Console type program will be outputted to Console Window or Terminal.
- System.out and System.err of Windows GUI type program will not be outputted.
- System.out of Service type program will not be outputted, System.err will be outputted to a disk file.
The configure items 'stdout' and 'stderr' are respectively used to redirect System.out and System.err to a file or a pipe. Error messages and Exceptions are outputted through System.err by default.
To a File
If the configure item 'stdout' or 'stderr' is followed by a file path, the redirect target is a file.
You can use a relative path or an absolute path. If the file path is a relative path, it is based on the "current working directory". For example:
forcecwd . stdout logs/system.out.log stderr logs/system.err.log
The first line is to make sure the "working directory" is the directory of generated exe.
To a Pipe
If the configure item 'stdout' or 'stderr' is followed by a '|' and a 'command line', the target is a pipe.
The command line should be executable from the "current working directory". It is strongly recommended to use 'cronolog' to split the output into daily or monthly logs.
For example, if your program is "/usr/folder/appdir/bin/program", the "cronolog" is at "/usr/folder/appdir/bin/cronolog", then:
forcecwd .. stdout |bin/cronolog logs/system.out.%Y%m%d.log stderr |bin/cronolog logs/system.err.%Y%m%d.log
The first line is to set the "working directory" to "/usr/folder/appdir/", from this "working directory", the "cronolog" is at "bin/cronolog", the file will be created in the directory "/usr/folder/appdir/logs/".
- cronolog.org - home page of cronolog
- See Force CWD page to know more about setting working directory.
- See Hello Stdout Redirect page for demo of stdout redirection.
About console configuration
Is it possible to make a Console Application (Windows) without open a new console when the app starts? This would be similar to how it works in Linux.
The problem is if I select Windows GUI Application, no output is shown when I start it from the console, this can be very useful in client/server applications.
Problems with stdout with Windows GUI applicaiton
I have created the exe file with the Windows GUI application type, also in the Config External, I set the stdout and stderr to two different log files. Now the problem is, the stderr is working for me, i.e., it prints out the exceptions in that file but for stdout, only the file is created, none of the System.out.println() are not working on GUI type exe. Where as, it works perfectly on the console type Application. The only problem which I have with the console type application is that there is always a black console window open with the application.
Is there anyway to fix this?
There is a temporary way to do this
I'm sorry for the problem and thank you for your report. I tried and this is a strange problem.
Now there is a temporary method, that you can do the redirect within Java code. At the beginning of your java program, you can set redirection by:
System.setOut(new PrintStream(new FileOutputStream("out.log")));
Thank you again for your report.
No problem :) It's just a small bug..
and yes, I applied that solution right away when I found out that problem. Right now I'm going to continue with just redirecting my stdout and stderr from my code itself.
Hi. I wonder if you can disable the generation of logs through stdout and stderr files.
Yes, you can disable it by redirecting to a NUL device
Yes, You can redirect stdout or stderr or both to a NUL device, on Windows:
Add new comment