devxlogo

Append an Exception Trace to a Log File

Append an Exception Trace to a Log File

If you want all exceptions to go to the same file, use this code:

 import java.io.*;import java.util.Date;public class Logger{public static synchronized logException(Exception ex){try {RandomAccessFile ff = new RandomAccessFile("c:\name.log","rw");// to append - move file pointerff.seek(ff.length());PrintWriter logWriter = new PrintWriter(new FileWriter(ff.getFD()));logWriter.println("============="+  (new Date()).toString() +ex.getMessage() + "========");ex.printStackTrace(logWriter);logWriter.println("=============================================");logWriter.close();}catch(Exception ee){//nothing to do}}}Use it as following:try{...}catch(Exception ex){ Logger.logException(ex); }

devx-admin

Share the Post: