Edit the source code and add an uncaught exception handler to every thread.
Looks something like this:
package the.package.here;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) throws Exception {
updateUncaughtExceptionHandlers();
... rest of the code is here
}
static void updateUncaughtExceptionHandlers() {
Thread.UncaughtExceptionHandler timestamped = (t, e) -> {
String timestamp = ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.err.printf("[%s] Exception in thread \"%s\" ",
timestamp, t.getName());
e.printStackTrace(System.err);
};
Thread.setDefaultUncaughtExceptionHandler(timestamped);
}
}
This will take care of uncaught exceptions, but won't take care of anything written out via System.err
or System.out
.
To take care of those, you'd use System.setErr()
and System.setOut()
, making PrintWriters that simply 'wrap' around the original System.out
and System.err
, and add a timestamp anytime a flag is true, then sets the flag false. The flag starts as true and is re-set to true every time you print a newline symbol.
I can show you how to do that, too, but before delving any deeper - can you change the source code? If you can't, your only option is to redirect standard out and standard err not to a file but pipe it to a different executable that adds these timestamps.