Java record log (exception record)

Source: Internet
Author: User
Tags stringbuffer
/** Test **/
@Test
public void Test () throws exception{
String msg = "Success";
try{
int[] is = null;
System.out.println (is[10]);
}catch (Exception e) {
E.printstacktrace ();
Simpletoof.setrecord (Simpletoof.getexceptionmsg (e));
msg = "Fail";
}finally{
SYSTEM.OUT.PRINTLN (msg);
}

}


Simpletoof.class

/** Record Operation History **/
public static void Setrecord (String record) {
String Recordfilepath = "D://record/record" +gettypedate ("Yyyy-mm-dd", New Date ()) + ". txt";
System.out.println (Recordfilepath);
File filename = new file (Recordfilepath);
try{
if (!filename.exists ())
Filename.createnewfile ();
FileWriter FileWriter = new FileWriter (Recordfilepath, true); True represents an Append
Filewriter.write (Gettypedate ("YyyyMMdd HH:mm:ss", New Date ()) + "\ r \ n" +record+ "\ r \ n");
Filewriter.flush ();
Filewriter.close ();
}catch (Exception ex) {
Ex.printstacktrace ();
}
}


/** Get exception information **/
public static String getexceptionmsg (Exception e) {
StringBuffer emsg = new StringBuffer ();
if (e!=null) {
stacktraceelement[] st = E.getstacktrace ();
for (Stacktraceelement stacktraceelement:st) {
String Exclass = Stacktraceelement.getclassname ();
String method = Stacktraceelement.getmethodname ();
Emsg.append ("[Class: + Exclass +"] Call "+ Method +" When in the first "+ Stacktraceelement.getlinenumber ()
+ "Line code place exception!" Exception type: "+ e.tostring () +" \ r \ n ");
}
}
return emsg.tostring ();
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.