In java, when we catch a exception, we usually can use printStackTrace() method to print the error information, and we can also use printStackTrace(PrintStream out) to direct those information to a file.
But how can I append those information into a existing file, just like using out.append()?
You must open file in append mode:
If you are not using Java 7, you must remember about closing or at least flushing the
Writer. Or you can have a globalWriter, but then you must synchronize it between threads.What about simply using some existing Java library like logback, log4j or even
java.util.logging? Simply say:…and the framework will log the exception wherever you want, with lots of additional data like thread name, timestamp, additional message, etc. Logback can also show you from which library given stack frame comes from and even print the stack trace starting from root cause (most nested exception).