利用异常类记录调用日志

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值