I need to monitor console output in Java, I have tried several different ways of retrieving the output as it is streamed, but I fell into a few pitfalls (looping the same output, only one part of output being captured, losing output) and have decided to go about it a different way.
I am developing a Java plugin for a server of the popular game, Minecraft, and I need to be able to monitor console output from other plugins.
I think a good way to do this is by redirecting console output to file, and then set up a recurring async task that checks the file, carries out anything it needs to do, and then clears the file for more input. I think I can do this with a simple use of System.setOut(PrintStream stream); and one of the many guides I can find on Google.
But there is a problem, and that is why I am asking here today. I need console output to stay on the console like normal, hence "mirroring". I cannot edit any of the plugins to output somewhere else, it needs to be from what my Java plugin can do and that only. Sure, maybe on every scheduled check on the file I could reprint it all back to the console but that would result in blocks being printed at a time, which would not be ideal. I do hope this is possible. Thank you!
EDIT: I don't think I explained fully. "plugins"'s classes are run by the program that I am developing for. I cannot change how classes are run and I cannot change how other plugins print to console.
I also do not want to direct console output to files for logging, that idea is only to keep it in a temporary place while it is parsed. Ideally, I need to pass each line of console output to a function (parseString(String line)
) which carries out operations depending on the contents of the line. Also, I don't understand how I can read stream contents line-by-line properly, so if someone has any idea on how to, please let me know. :)

- 583
- 6
- 24
-
`java ... foo.jar | tee output.txt` – Paul Tomblin Aug 10 '12 at 20:07
-
How are you running this? On a Linux platform from the console, you can pipe output into `tee`, which does this work of printing to a file as well as the console. – Carl Aug 10 '12 at 20:07
-
ohey, a minecraft modder. I am also one of those. – Wug Aug 10 '12 at 20:07
-
@Paul: I cannot do that, as the plugins are actually run after the main program starts running, and I can't edit that either. Carl: See my message to paul please, Wug: Minecraft Bukkit plugin development, not modding :) – toficofi Aug 10 '12 at 20:18
-
See this also: http://stackoverflow.com/questions/741775/capture-console-output-of-a-specific-thread-in-java?rq=1 – Richard Sitze Aug 10 '12 at 20:29
3 Answers
Capture System.out before changing it, and use TeeOutputStream:
OutputStream myCaptureStream = ...;
PrintStream original = System.out;
OutptStream outputtee = new TeeOutputStream(originalOut, myCaptureStream);
PrintStream printTee = new PrintStream(outputTee);
System.setOut(printTee);
Convert output stream (myCaptureStream) to an input stream
- Use Java standard library PipedOutputStream:
new PipedOutputStream
; read carefully - you'll need your reader running on another thread. - Convert the input stream to a Reader,
- and that to a BufferedReader.
BufferedReader gives you a readLine
method.
Pseudo code:
// Feed myCaptureStream to TeeOutputStream
OutputStream myCaptureStream = new PipedOutputStream();
// Prepare to capture data being written to that output stream
InputStream myCaptureAsInputStream = new PipedInputStream(myCaptureStream);
Reader myCaptureReader = new InputStreamReader(myCaptureAsInputStream);
BufferedReader myCaptureBuffered = new BufferedReader(myCaptureReader, 1024);
// This must run on separate reader thread; in spin loop:
myCaptureBuffer.readLine

- 8,262
- 3
- 36
- 48
-
TeeOutputStream is a great solution so +1, but pulling in all of apache commons for this one thing seems possibly suboptimal. – Wug Aug 10 '12 at 20:23
-
Apache Commons is both small and optimized, and also nicely split up into sub-components. – Paul Tomblin Aug 10 '12 at 20:24
-
@Jishaxe I googled for "java stream line-by-line" and found http://www.geekality.net/2011/05/23/java-inputstream-line-iterator/ – Richard Sitze Aug 10 '12 at 20:44
-
You can change the System.out and System.err to be references to your own custom PrintStreams. This solution is cross platform, unlike using a platform specific helper executable such as tee
.
Documentation for System.setOut(PrintStream)
You can write your own PrintStream Subclass that looks something like:
class MirroringPrintStream extends PrintStream
{
PrintStream first, second;
public MirroringPrintStream(PrintStream first, PrintStream second)
{
// fail now rather than later
if (first == null || second == null) throw new NullPointerException();
this.first = first;
this.second = second;
}
@override
// methods go here (I do believe there are rather a lot of them, but they will all look the same)
}
You can then use System.setOut(new MirroringPrintStream(System.out, myLogStream))
where myLogStream
is the PrintStream you opened to your log file. I'm not sure how this will handle appending to a file that something else periodically truncates, you might want to experiment with that a bit.
All else fails, go to irc.esper.net and ask about it in #risucraft

- 12,956
- 4
- 34
- 54
-
-
Note that this won't actually compile, you'll need to do some work. First of all, PrintStream has no default constructor, so you'll have to fake it. It might be easier to create a regular PrintStream and pass its constructor your own custom OutputStream that handles the mirroring, as with OutputStream you only have to worry about I think 2 methods. – Wug Aug 10 '12 at 20:21
-
-
I'd also like to add that one advantage of this approach is that you can later replace the file-outbound stream to anything else, e.g. a stream that goes over the Internet to a server of yours. Or sends you an e-mail with the digest... the possibilities are considerable. – mikołak Aug 10 '12 at 20:24
Assuming this is a friendly takeover of console output, why not log to your own stream and mirror that to the console?

- 1,792
- 19
- 26
-
That's what I tried before, and failed at :) I wasn't sure how to get lines from the stream as they come through. If you can supply me with sample code or a link to a guide I might've missed, feel free to do so ;) – toficofi Aug 10 '12 at 20:21
-
define your own class which implements an appropriate protocol, like "void logMessage(String str)" and which logs the messages to both your own file and System.out. Use this instead of a raw stream as the target of your console activity – ddyer Aug 11 '12 at 00:53