Salutations,
I am creating a logging system for MERCury, and I want to be able to save all of the console output into a file. Is there a way to take all of what was put into System.out, and print it to a file?
Thanks,
-wes
Salutations,
I am creating a logging system for MERCury, and I want to be able to save all of the console output into a file. Is there a way to take all of what was put into System.out, and print it to a file?
Thanks,
-wes
perhaps use a logging library?
try Log4J
You can try this, have served me fine for over a year.
public class SplitPrintStream extends PrintStream {
private PrintStream second;
public SplitPrintStream(PrintStream main, PrintStream second) {
super(main);
this.second = second;
}
@Override
public void close() {
super.close();
if (second != null)
second.close();
}
@Override
public void flush() {
super.flush();
if (second != null)
second.flush();
}
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
if (second != null)
second.write(buf, off, len);
}
@Override
public void write(int b) {
super.write(b);
if (second != null)
second.write(b);
}
@Override
public void write(byte[] b) throws IOException {
super.write(b);
if (second != null)
second.write(b);
}
}
Simply pass it System.out and the printstream for your log file or wherever else, you want to sent the console output(I use it to read console output, from my play testers via network.),
then replace System.out with the new SplitPrintStream using System.setOut().
You can also just call this at the start of your program.
try
{
PrintStream out = new PrintStream("log.txt");
System.setOut(out);
System.setErr(out);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
This makes all [icode]System.out[/icode] calls output to the file as well as [icode]System.err[/icode] messages and stack traces. It has worked fine for me with all my projects.
Just wrote a little class for splitting a PrintStream into two other ones so you could use Longarmx’s way to write every console output to both, the file AND the normal console (or more).
import java.io.PrintStream;
public class SplitPrintStream extends PrintStream
{
private PrintStream[] printStreams;
public SplitPrintStream(PrintStream[] printStreams)
{
super(printStreams[0]);
this.printStreams = printStreams;
}
public void print(String s) { for(int i=0;i<this.printStreams.length;i++) this.printStreams[i].print(s);}
public void print(char c) { for(int i=0;i<this.printStreams.length;i++) this.printStreams[i].print(c);}
public void print(Object o) { for(int i=0;i<this.printStreams.length;i++) this.printStreams[i].print(o);}
//etc
public void println(String s) { for(int i=0;i<this.printStreams.length;i++) this.printStreams[i].println(s);}
public void println(char c) { for(int i=0;i<this.printStreams.length;i++) this.printStreams[i].println(c);}
public void println(Object o) { for(int i=0;i<this.printStreams.length;i++) this.printStreams[i].println(o);}
//etc
}
@all pls stop this hackery reinventing of the wheel
The things you try to do are already solved by logging libraries, there is also one already included in the standard JRE (java.util.logging).
@Danny02
Sometimes it is just easier to make something yourself than understanding a whole new API of which you need only one little feature from.