Search in sources :

Example 1 with SqlExecuteTrace

use of org.sagacity.sqltoy.model.SqlExecuteTrace in project sagacity-sqltoy by chenrenfei.

the class SqlExecuteStat method loggerSql.

private static void loggerSql() {
    try {
        SqlExecuteTrace sqlTrace = threadLocal.get();
        if (sqlTrace == null)
            return;
        long overTime = sqlTrace.getExecuteTime() - printSqlTimeoutMillis;
        if (overTime >= 0 && sqlTrace.getStart() != null)
            logger.warn("类型:{}的sql执行超出:{}毫秒的阀值, 共执行:{} 毫秒,请优化!", sqlTrace.getType(), printSqlTimeoutMillis, overTime + printSqlTimeoutMillis);
        else if (!sqlTrace.isError())
            return;
        List<SqlToyResult> sqlToyResults = sqlTrace.getSqlToyResults();
        for (SqlToyResult sqlResult : sqlToyResults) printSql(sqlResult.getSql(), sqlResult.getParamsValue(), true);
    } catch (Exception e) {
    }
}
Also used : SqlExecuteTrace(org.sagacity.sqltoy.model.SqlExecuteTrace) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 2 with SqlExecuteTrace

use of org.sagacity.sqltoy.model.SqlExecuteTrace in project sagacity-sqltoy by chenrenfei.

the class SqlExecuteStat method printSql.

/**
 * @todo 实际执行打印sql和参数
 * @param sql
 * @param paramValues
 * @param isLogger
 */
private static void printSql(String sql, Object[] paramValues, boolean isLogger) {
    SqlExecuteTrace sqlTrace = threadLocal.get();
    StringBuilder paramStr = new StringBuilder();
    if (paramValues != null) {
        for (int i = 0; i < paramValues.length; i++) {
            if (i > 0)
                paramStr.append(",");
            paramStr.append("p[" + i + "]=" + paramValues[i]);
        }
    }
    if (sqlTrace != null) {
        if (isLogger) {
            logger.error("执行:{} 类型的sql,sqlId={}", sqlTrace.getType(), sqlTrace.getId() + " 发生异常!");
        } else
            out.println("执行:{" + sqlTrace.getType() + "} 类型sql,sqlId=" + sqlTrace.getId());
    }
    if (isLogger) {
        logger.error("sqlScript:{}", sql);
        logger.error("sqlParams:{}", paramStr);
    } else {
        out.println("sqlScript:" + sql);
        out.println("sqlParams:" + paramStr);
    }
}
Also used : SqlExecuteTrace(org.sagacity.sqltoy.model.SqlExecuteTrace)

Aggregations

SqlExecuteTrace (org.sagacity.sqltoy.model.SqlExecuteTrace)2 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)1