I know that it is possible that every line you print can be tagged with date (and also saved as a log file).
For example in Minecraft:
[16:21:43 INFO]: Minecraft Launcher 1.3.9 (through bootstrap 5) started on windows...
How do I do that? Maybee with Logger? Or are external librarys required?
It's possible to show dates prefixed in your String by calling System.out.println
PrintStream
classprintln
method to prefix the String with the dateSystem.setOut
with your custom PrintStream
Custom stream:
public class MyPrintStream extends PrintStream {
public MyPrintStream(OutputStream out) {
super(out);
}
@Override
public void println(String string) {
Date date = new Date();
super.println("[" + date.toString() + "] " + string);
}
}
Test class:
public static void main(String[] args) {
System.setOut(new MyPrintStream(System.out));
System.out.println("Hello World!");
System.out.println("Yellow World!");
}
Output:
[Mon Feb 10 21:10:14 IST 2014] Hello World!
[Mon Feb 10 21:10:14 IST 2014] Yellow World!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With