明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理
/**
     * 过滤不重要的StackTrace,只保留与项目相关的StackTrace
     *
     */
    public static String getExceptionStackTrace(Throwable e) {
        return getExceptionStackTrace(e, "com\\.(xiaonuo|runqin|sdk|onecode).*", "xiaonuo|baomidou");
    }

    /**
     * 过滤不重要的StackTrace,只保留与项目相关的StackTrace
     *
     * @throws Exception
     */
    public static String getExceptionStackTrace(Throwable e, String includesRegex, String excludes) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String result = sw.toString();
        boolean[] firstTrackLine = new boolean[]{true};
        if (includesRegex != null) {
            result = (new RegexReplace(result, "(    at )(.*)(\r?\n)") {
                @Override
                public String getReplacement(Matcher matcher) {
                    if (firstTrackLine[0]) {
                        firstTrackLine[0] = false;
                        return matcher.group();
                    }
                    else if (matcher.group(2).matches(includesRegex))
                        return matcher.group();
                    else
                        return "";
                }
            }).replaceAll();
        }
        return result;
    }
import java.util.regex.Matcher;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

abstract public class RegexReplace {
    private String _regex,_source;
    private int _flags=0;//Pattern.CASE_INSENSITIVE|Pattern.DOTALL;
    public RegexReplace(String source,String regex){
        this._regex=regex;this._source=source;
    }
    public RegexReplace(String source,String regex,int flags) {
        this(source,regex);
        this._flags=flags;
    }

    abstract public String getReplacement(Matcher matcher);

    public String replaceAll(){
        Pattern p=Pattern.compile(_regex,_flags);
        Matcher m=p.matcher(_source);
        StringBuffer result=new StringBuffer();
        while(m.find()){
            // System.out.println(content.substring(m.start(),m.end()));
            // System.out.println(m.group(2));
            String replacement=getReplacement(m);
            if(replacement!=null){
                if(replacement.indexOf('$')>=0)
                    replacement=replacement.replace("$","\\$");
                //replacement=EasyStr.ReplaceAll(replacement,"$","\\$",false);
                m.appendReplacement(result,replacement);
            }
        }
        m.appendTail(result);
        return result.toString();
    }

    public String replaceFirst(){
        Pattern p=Pattern.compile(_regex,_flags);
        Matcher m=p.matcher(_source);
        StringBuffer result=new StringBuffer();
        if(m.find()){
            // System.out.println(content.substring(m.start(),m.end()));
            // System.out.println(m.group(2));
            String replacement=getReplacement(m);
            if(replacement!=null){
                if(replacement.indexOf('$')>=0)
                    replacement=replacement.replace("$","\\$");
                //System.out.println(replacement);
                m.appendReplacement(result,replacement);
            }
        }
        m.appendTail(result);
        return result.toString();
    }
}

 

调用:String sMsg = CommonExceptionUtil.getExceptionStackTrace(e);