利用异常类记录调用日志

来源:互联网 发布:数据挖掘 软件 编辑:程序博客网 时间:2024/06/11 01:45
package com.qingyuan.proxy;import java.io.ByteArrayOutputStream;import java.io.PrintStream;import java.net.InetAddress;import java.net.UnknownHostException;import org.junit.Test;/** * Throwable java 中强大的异常处理功能, 在开发中可以得到很好的体现, 我们在多线程任务执行的时候可以 * 利用此类, 记录详细的执行日志<轨迹>,可以生成一张日志表; * 得到调用方法的全部信息,方便定位问题,在代码多少行就可以明确指定,是查询和定位必备之良品; *  * 夯实java 基础知识:  * StackTraceElement[] stacks = new Throwable().getStackTrace();  * InetAddress * process = Runtime.getRuntime().exec(); |  process.waitFor(); * long startexecutiveTime = System.currentTimeMillis(); */public class ExceptionClassDigger{    public static void timerBegin()    {        try        {               // 文件执行栈元素对象 <[java.lang.StackTraceElement]> public final class StackTraceElement implements java.io.Serializable            StackTraceElement[] stacks = new Throwable().getStackTrace();                String className = stacks[1].getClassName();            String methodName = stacks[1].getMethodName();            stacks[0].getClassName(); // com.qingyuan.proxy.ResoucePathChecker            stacks[0].getFileName();  // ResourcePathChecker.java            stacks[0].getMethodName(); // timerBegin()                     stacks[1].getClassName();  // com.qingyuan.proxy.ResoucePathChecker            stacks[1].getFileName(); // ResourcePathChecker.java            stacks[1].getMethodName(); // testPath()            stacks[1].getLineNumber(); // 73             InetAddress localhost = null;            String ipaddress = "";                          try             {                localhost = InetAddress.getLocalHost();                ipaddress = localhost.getHostAddress();            }             catch (UnknownHostException e)             {                e.printStackTrace();            }             // com.qingyuan.proxy.ResourcePathChecker : testPath : XWX192794C/10.45.0.234 : 10.45.0.234................            System.out.print(className + "\n" + methodName + "\n" + localhost + "\n" + ipaddress);            System.out.println("................");                        /*TimerMonitoring timerMonitoring = new TimerMonitoring();            timerMonitoring.setSerial(serial);            timerMonitoring.setBegintime(new Date());            timerMonitoring.setState(TimerMonitoringConfig.TIMER_MONITORING_STATE_CODE2);            timerMonitoring.setClassName(className);            timerMonitoring.setMethodname(methodName);            timerMonitoring.setIpaddress(ipaddress);            TimerMonitoringService timerMonitoringService = (TimerMonitoringService)ContextHolder.getBean("c_smt_timerMonitoringService");            timerMonitoringService.insert(timerMonitoring);*/        }        catch (Exception e) { }             }         /**     * 将异常信息记录, 得到具体是什么类型的异常,比如: <[java.lang.NullPointerException]>     * @param e     * @return String     */    public static String exceptionToString(Exception e)    {           if(e == null)           {             return null;            }         ByteArrayOutputStream baos = new ByteArrayOutputStream();           try          {             e.printStackTrace(new PrintStream(baos)); // 将输出流         }          catch (Exception ex)          {            ex.printStackTrace();         }        String result = "";        String eee =  baos.toString();            /*eee= "java.lang.NullPointerException : null pointer here            at com.qingyuan.proxy.ExceptionClassDigger.testPath(ExceptionClassDigger.java:120)            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)            at java.lang.reflect.Method.invoke(Method.java:597) ";*/                if(!"".equals(baos.toString()))        {            String[] str = baos.toString().split(":");            result = str[0];        }                System.out.println(result); // java.lang.NullPointerException        return result;       }        public boolean equals(Object obj)     {        if (obj==this)        {            return true;        }        if (!(obj instanceof StackTraceElement))        {            return false;        }        StackTraceElement e = (StackTraceElement)obj;               /* return e.declaringClass.equals(declaringClass) && e.lineNumber == lineNumber        && eq(methodName, e.methodName) && eq(fileName, e.fileName);*/        return true;    }        @Test    public  void testPath()     {        timerBegin();                exceptionToString(new NullPointerException("null pointer here"));    }}

0 0