import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream ;
import java.sql.Timestamp;
/**
* This class allow replaces standard output and error with a print stream that
* copies its output to both the console and to a file.
*/
public class IpsStandardStream extends PrintStream {
protected PrintStream out;
*/
public IpsStandardStream(PrintStream out1, PrintStream out2) {
super(out1);
this.out = out2;
}
public void write(byte buf[], int off, int len) {
try {
super.write(buf, off, len);
out.write(buf, off, len);
} catch (Exception e) {
}
}
public void flush() {
super.flush();
out.flush();
}
*
* @version 1.0
*/
public static void replaceStandardOutput() {
try {
PrintStream outPut = new PrintStream(
new FileOutputStream(" out.log"));
PrintStream tee = new IpsStandardStream(System.out, outPut);
System.setOut(tee);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
*
* @version 1.0
*/
public static void replaceStandardError() {
try {
PrintStream err = new PrintStream(new FileOutputStream("error.log"));
PrintStream tee = new IpsStandardStream(System.err, err);
System.setErr(tee);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] arg) {
IpsStandardStream.replaceStandardOutput();
IpsStandardStream.replaceStandardError();
// Write to standard output and error and the log files
Timestamp now;
for (int i = 0; i < 1; i++) {
now = new Timestamp( System.currentTimeMillis());
System.out.println(now);
System.out.println("welcome " + i);
System.err.println(now);
System.err.println("error " + i);
}
}
}