devxlogo

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); }

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Seasoned Architects Evaluate New Tech

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.