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*;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;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); }
See also  Is Machine Learning Automating Creativity in Graphic Design?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist