You can change the PrintStream , which is used as standard output:
System.setOut (PrintStream out)
Create your own PrintStream , which prints any additional information that you want (old) standard output, and set it with:
System.setOut(myStream);
Example:
The following example prints the current millimeters of time before each printed String , which is printed using the PrintStream.println(String x) method:
PrintStream myStream = new PrintStream(System.out) { @Override public void println(String x) { super.println(System.currentTimeMillis() + ": " + x); } }; System.setOut(myStream); System.out.println("Hello World!");
Conclusion:
1420553422337: Hello World!
Note:
This example only overrides the PrintStream.println(String x) method, so calling other PrintStream printing methods will not add a timestamp to the output.
source share