⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p6logquery.java

📁 一个跟踪调试jdbc的工具包。通过把jdbc做一个封装
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return array;
    }
    
    static protected void doLog(long elapsed, String category, String prepared, String sql) {
        doLog(-1, elapsed, category, prepared, sql);
    }
    
    // this is an internal method called by logElapsed
    static protected void doLogElapsed(int connectionId, long startTime, long endTime, String category, String prepared, String sql) {
        doLog(connectionId, (endTime - startTime), category, prepared, sql);
    }
    
    // this is an internal procedure used to actually write the log information
    static protected synchronized void doLog(int connectionId, long elapsed, String category, String prepared, String sql) {
	if (logger != null) {
	    java.util.Date now = P6Util.timeNow();
	    SimpleDateFormat sdf = P6SpyOptions.getDateformatter();
	    String stringNow;
	    if (sdf == null) {
		stringNow = Long.toString(now.getTime());
	    } else {
		stringNow = sdf.format(new java.util.Date(now.getTime())).trim();
	    }

	    logger.logSQL(connectionId, stringNow, elapsed, category, prepared, sql);


	    boolean stackTrace = P6SpyOptions.getStackTrace();
	    String stackTraceClass = P6SpyOptions.getStackTraceClass();
	    if(stackTrace) {
		Exception e = new Exception();
		if(stackTraceClass != null) {
		    StringWriter sw = new StringWriter();
		    PrintWriter pw  = new PrintWriter(sw);
		    e.printStackTrace(pw);
		    String stack = sw.toString();
		    if(stack.indexOf(stackTraceClass) != -1) {
			lastStack = stack;
		    } else {
			e = null;
		    }
		}
		if (e != null) {
		    logger.logException(e);
		}
	    }
        
	    //lastEntry = logEntry;
	}

	/*
	java.util.Date now = P6Util.timeNow();
	SimpleDateFormat sdf = P6SpyOptions.getDateformatter();
	String logEntry;
	if (sdf == null) {
	    logEntry = Long.toString(now.getTime());
	} else {
	    logEntry = sdf.format(new java.util.Date(now.getTime())).trim();
	}

        logEntry += "|"+elapsed+"|"+(connectionId==-1 ? "" : String.valueOf(connectionId))+"|"+category+"|"+prepared+"|"+sql;
        qlog.println(logEntry);
        boolean stackTrace = P6SpyOptions.getStackTrace();
        String stackTraceClass = P6SpyOptions.getStackTraceClass();
        if(stackTrace) {
            if(stackTraceClass == null) {
                Exception e = new Exception();
                e.printStackTrace(qlog);
            }
            else {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                PrintStream printStream = new PrintStream(byteStream);
                Exception e = new Exception("P6Spy Stack Trace Log");
                e.printStackTrace(printStream);
                String stack = byteStream.toString();
                if(stack.indexOf(stackTraceClass) != -1) {
                    lastStack = stack;
                    e.printStackTrace(qlog);
                }
            }
        }
        
        lastEntry = logEntry;
	*/
    }
    
    static boolean isLoggable(String sql) {
        return(P6SpyOptions.getFilter() == false || queryOk(sql));
    }
    
    static boolean isCategoryOk(String category) {
        return (includeCategories == null || includeCategories.length == 0 || foundCategory(category,includeCategories)) && !foundCategory(category,excludeCategories);
    }
    
    static boolean foundCategory(String category, String categories[]) {
        if (categories != null) {
            for (int i = 0; i < categories.length; i++) {
                if (category.equals(categories[i])) {
                    return true;
                }
            }
        }
        return false;
    }
    
    static boolean queryOk(String sql) {
        if(P6SpyOptions.getStringMatcherEngine() != null && P6SpyOptions.getSQLExpression() != null) {
            return sqlOk(sql);
        }
        else
            return ((includeTables == null || includeTables.length == 0 || foundTable(sql, includeTables))) && !foundTable(sql, excludeTables);
    }
    
    static boolean sqlOk(String sql) {
        String sqlexpression = P6SpyOptions.getSQLExpression();
        try {
            return P6SpyOptions.getStringMatcherEngine().match(sqlexpression, sql);
        }
        catch (MatchException e) {
            P6LogQuery.logError("Exception during matching sqlexpression [" + sqlexpression + "] to sql [" + sql + "]: ");
            return false;
        }
        
    }
    static boolean foundTable(String sql, String tables[]) {
        sql = sql.toLowerCase();
        boolean ok = false;
        int i;
        if (tables != null) {
            for (i = 0; !ok && i < tables.length; i++) {
                ok = tableOk(sql, tables[i]);
            }
        }
        
        return ok;
    }
    
    static boolean tableOk(String sql, String table) {
        try {
            return P6SpyOptions.getStringMatcherEngine().match(table, sql);
        }
        catch (MatchException e) {
            P6LogQuery.logError("Exception during matching expression [" + table + "] to sql [" + sql + "]: ");
            return false;
        }
    }
    
    // ----------------------------------------------------------------------------------------------------------
    // public accessor methods for logging and viewing query data
    // ----------------------------------------------------------------------------------------------------------
    
    static public void clearLastStack() {
        lastStack = null;
    }
    
    static public String getLastEntry() {
        //return lastEntry;
	return logger.getLastEntry();
    }
    
    static public String getLastStack() {
        return lastStack;
    }
    
    static public String[] getIncludeTables() {
        return includeTables;
    }
    
    static public String[] getExcludeTables() {
        return excludeTables;
    }
    
    static public void setIncludeTables(String _includeTables) {
        P6LogQuery.includeTables = P6LogQuery.parseCSVList(_includeTables);
    }
    
    static public void setExcludeTables(String _excludeTables) {
        P6LogQuery.excludeTables = P6LogQuery.parseCSVList(_excludeTables);
    }
    
    static public void setIncludeCategories(String _includeCategories) {
        P6LogQuery.includeCategories = P6LogQuery.parseCSVList(_includeCategories);
    }
    
    static public void setExcludeCategories(String _excludeCategories) {
        P6LogQuery.excludeCategories = P6LogQuery.parseCSVList(_excludeCategories);
    }
    
    // this a way for an external to dump an unrestricted line of text into the log
    // useful for the JSP demarcation tool
    static public void logText(String text) {
	logger.logText(text);
        //qlog.println(text);
    }
    
    static public void log(String category, String prepared, String sql) {
        //if (qlog != null) {
        if (logger != null) {
            doLog(-1, category, prepared, sql);
        }
    }
    
    static public void logElapsed(int connectionId, long startTime, String category, String prepared, String sql) {
	logElapsed(connectionId, startTime, System.currentTimeMillis(), category, prepared, sql);
    }
    
    static public void logElapsed(int connectionId, long startTime, long endTime, String category, String prepared, String sql) {
    	if (logger != null && meetsThresholdRequirement(endTime-startTime) && isLoggable(sql) && isCategoryOk(category)) {
		doLogElapsed(connectionId, startTime, endTime, category, prepared, sql);
	} else if (isDebugOn()) {
		logDebug("P6Spy intentionally did not log category: "+category+", statement: "+sql+"  Reason: logger="+logger+", isLoggable="+isLoggable(sql)+", isCategoryOk="+isCategoryOk(category));
	}
    }

    //->JAW: new method that checks to see if this statement should be logged based
    //on whether on not it has taken greater than x amount of time.
    static private boolean meetsThresholdRequirement(long timeTaken) {
	    long executionThreshold = P6SpyOptions.getExecutionThreshold();

        if(executionThreshold <= 0) {
            return true;
        }
        else if(timeTaken > executionThreshold) {
            return true;
        }
        else {
            return false;
        }
    }
    //<-JAW
        
    static public void logInfo(String sql) {
        if (logger != null && isCategoryOk("info")) {
            doLog(-1, "info", "", sql);
        }
    }
    
    static public boolean isDebugOn() {
	return isCategoryOk("debug");
    }
    static public void logDebug(String sql) {
        if (isDebugOn()) {
            if (logger != null) {
                doLog(-1, "debug", "", sql);
            } else {
                System.err.println(sql);
            }
        }
    }

    static public void logError(String sql) {
        System.err.println ("Warning: "+sql);
        if (logger != null) {
            doLog(-1, "error", "", sql);
        }
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -