|
|
|
@ -1,143 +1,145 @@
|
|
|
|
|
package com.ibeetl.admin.core.conf;
|
|
|
|
|
|
|
|
|
|
import org.beetl.sql.core.InterceptorContext;
|
|
|
|
|
import org.beetl.sql.core.JavaType;
|
|
|
|
|
import org.beetl.sql.core.SQLManager;
|
|
|
|
|
import org.beetl.sql.core.mapper.MapperJavaProxy;
|
|
|
|
|
import org.beetl.sql.core.query.LambdaQuery;
|
|
|
|
|
import org.beetl.sql.core.query.Query;
|
|
|
|
|
import org.beetl.sql.ext.DebugInterceptor;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SQL输出到日志
|
|
|
|
|
*/
|
|
|
|
|
public class MyDebugInterceptor extends DebugInterceptor {
|
|
|
|
|
|
|
|
|
|
static Logger logger = LoggerFactory.getLogger(MyDebugInterceptor.class);
|
|
|
|
|
|
|
|
|
|
public static Boolean showBeetlsql = Boolean.parseBoolean(ResourceBundle.getBundle("application") .getString("beetl-beetlsql.dev"));
|
|
|
|
|
|
|
|
|
|
private String showName;
|
|
|
|
|
|
|
|
|
|
static String mapperName = MapperJavaProxy.class.getName();
|
|
|
|
|
static String sqlManagerName = SQLManager.class.getName();
|
|
|
|
|
static String queryClassName = Query.class.getName();
|
|
|
|
|
static String lambdaQueryName = LambdaQuery.class.getName();
|
|
|
|
|
|
|
|
|
|
public MyDebugInterceptor(String showName) {
|
|
|
|
|
super();
|
|
|
|
|
this.showName = showName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void println(String str) {
|
|
|
|
|
String lineSeparator = System.getProperty("line.separator", "\n");
|
|
|
|
|
// String lineSeparator ="";
|
|
|
|
|
if(showBeetlsql){
|
|
|
|
|
logger.info(lineSeparator+str);
|
|
|
|
|
}else {
|
|
|
|
|
System.out.println(lineSeparator+str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void before(InterceptorContext ctx) {
|
|
|
|
|
String sqlId = ctx.getSqlId();
|
|
|
|
|
if (this.isDebugEanble(sqlId)) {
|
|
|
|
|
ctx.put("debug.time", System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
if (this.isSimple(sqlId)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
String lineSeparator = System.getProperty("line.separator", "\n");
|
|
|
|
|
sb.append("┏━━━━━ "+showName+" Debug [").append(this.getSqlId(formatSql(sqlId))).append("] ━━━").append(lineSeparator)
|
|
|
|
|
.append("┣ SQL:\t " + formatSql(ctx.getSql()) + lineSeparator)
|
|
|
|
|
.append("┣ 参数:\t " + formatParas(ctx.getParas())).append(lineSeparator);
|
|
|
|
|
RuntimeException ex = new RuntimeException();
|
|
|
|
|
StackTraceElement[] traces = ex.getStackTrace();
|
|
|
|
|
int index = lookBusinessCodeInTrace(traces);
|
|
|
|
|
StackTraceElement bussinessCode = traces[index];
|
|
|
|
|
String className = bussinessCode.getClassName();
|
|
|
|
|
String mehodName = bussinessCode.getMethodName();
|
|
|
|
|
int line = bussinessCode.getLineNumber();
|
|
|
|
|
sb.append("┣ 位置:\t " + className + "." + mehodName + "(" + bussinessCode.getFileName() + ":" + line + ")"
|
|
|
|
|
+ lineSeparator);
|
|
|
|
|
ctx.put("logs", sb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected int lookBusinessCodeInTrace(StackTraceElement[] traces) {
|
|
|
|
|
|
|
|
|
|
String className = getTraceClassName();
|
|
|
|
|
for (int i = traces.length - 1; i >= 0; i--) {
|
|
|
|
|
String name = traces[i].getClassName();
|
|
|
|
|
if (className != null && className.equals(name)) {
|
|
|
|
|
return i;
|
|
|
|
|
} else if (name.equals(mapperName)) {
|
|
|
|
|
// 越过sun jdk 代理
|
|
|
|
|
int skipLine = JavaType.isJdk8() ? 3 : 2;
|
|
|
|
|
return i + skipLine;
|
|
|
|
|
} else if (name.equals(lambdaQueryName)) {
|
|
|
|
|
return i + 1;
|
|
|
|
|
} else if (name.equals(queryClassName)) {
|
|
|
|
|
return i + 1;
|
|
|
|
|
} else if (name.equals(sqlManagerName)) {
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 不可能到这里
|
|
|
|
|
throw new IllegalStateException();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void after(InterceptorContext ctx) {
|
|
|
|
|
String sqlId = ctx.getSqlId();
|
|
|
|
|
if (this.isSimple(sqlId)) {
|
|
|
|
|
this.simpleOut(ctx);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
|
long start = (Long) ctx.get("debug.time");
|
|
|
|
|
String lineSeparator = System.getProperty("line.separator", "\n");
|
|
|
|
|
StringBuilder sb = (StringBuilder) ctx.get("logs");
|
|
|
|
|
sb.append("┣ 时间:\t " + (time - start) + "ms").append(lineSeparator);
|
|
|
|
|
if (ctx.isUpdate()) {
|
|
|
|
|
sb.append("┣ 更新:\t [");
|
|
|
|
|
if (ctx.getResult().getClass().isArray()) {
|
|
|
|
|
int[] ret = (int[]) ctx.getResult();
|
|
|
|
|
for (int i = 0; i < ret.length; i++) {
|
|
|
|
|
if (i > 0)
|
|
|
|
|
sb.append(",");
|
|
|
|
|
sb.append(ret[i]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
sb.append(ctx.getResult());
|
|
|
|
|
}
|
|
|
|
|
sb.append("]").append(lineSeparator);
|
|
|
|
|
} else {
|
|
|
|
|
if (ctx.getResult() instanceof Collection) {
|
|
|
|
|
sb.append("┣ 结果:\t [").append(((Collection) ctx.getResult()).size()).append("]").append(lineSeparator);
|
|
|
|
|
} else {
|
|
|
|
|
sb.append("┣ 结果:\t [").append(ctx.getResult()).append("]").append(lineSeparator);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append("┗━━━━━ Debug [").append(this.getSqlId(formatSql(ctx.getSqlId()))).append("] ━━━")
|
|
|
|
|
.append(lineSeparator);
|
|
|
|
|
println(sb.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getShowName() {
|
|
|
|
|
return showName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setShowName(String showName) {
|
|
|
|
|
this.showName = showName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//package com.ibeetl.admin.core.conf;
|
|
|
|
|
//
|
|
|
|
|
//import org.beetl.sql.clazz.kit.JavaType;
|
|
|
|
|
//import org.beetl.sql.core.InterceptorContext;
|
|
|
|
|
//import org.beetl.sql.clazz.kit.JavaType;
|
|
|
|
|
//import org.beetl.sql.core.SQLManager;
|
|
|
|
|
//import org.beetl.sql.mapper.MapperJavaProxy;
|
|
|
|
|
//import org.beetl.sql.core.query.LambdaQuery;
|
|
|
|
|
//import org.beetl.sql.core.query.Query;
|
|
|
|
|
//import org.beetl.sql.ext.DebugInterceptor;
|
|
|
|
|
//import org.beetl.sql.mapper.MapperJavaProxy;
|
|
|
|
|
//import org.slf4j.Logger;
|
|
|
|
|
//import org.slf4j.LoggerFactory;
|
|
|
|
|
//
|
|
|
|
|
//import java.util.Collection;
|
|
|
|
|
//import java.util.ResourceBundle;
|
|
|
|
|
//
|
|
|
|
|
///**
|
|
|
|
|
// * SQL输出到日志
|
|
|
|
|
// */
|
|
|
|
|
//public class MyDebugInterceptor extends DebugInterceptor {
|
|
|
|
|
//
|
|
|
|
|
// static Logger logger = LoggerFactory.getLogger(MyDebugInterceptor.class);
|
|
|
|
|
//
|
|
|
|
|
// public static Boolean showBeetlsql = Boolean.parseBoolean(ResourceBundle.getBundle("application") .getString("beetl-beetlsql.dev"));
|
|
|
|
|
//
|
|
|
|
|
// private String showName;
|
|
|
|
|
//
|
|
|
|
|
// static String mapperName = MapperJavaProxy.class.getName();
|
|
|
|
|
// static String sqlManagerName = SQLManager.class.getName();
|
|
|
|
|
// static String queryClassName = Query.class.getName();
|
|
|
|
|
// static String lambdaQueryName = LambdaQuery.class.getName();
|
|
|
|
|
//
|
|
|
|
|
// public MyDebugInterceptor(String showName) {
|
|
|
|
|
// super();
|
|
|
|
|
// this.showName = showName;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// protected void println(String str) {
|
|
|
|
|
// String lineSeparator = System.getProperty("line.separator", "\n");
|
|
|
|
|
//// String lineSeparator ="";
|
|
|
|
|
// if(showBeetlsql){
|
|
|
|
|
// logger.info(lineSeparator+str);
|
|
|
|
|
// }else {
|
|
|
|
|
// System.out.println(lineSeparator+str);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// public void before(InterceptorContext ctx) {
|
|
|
|
|
// String sqlId = ctx.getSqlId();
|
|
|
|
|
// if (this.isDebugEanble(sqlId)) {
|
|
|
|
|
// ctx.put("debug.time", System.currentTimeMillis());
|
|
|
|
|
// }
|
|
|
|
|
// if (this.isSimple(sqlId)) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// StringBuilder sb = new StringBuilder();
|
|
|
|
|
// String lineSeparator = System.getProperty("line.separator", "\n");
|
|
|
|
|
// sb.append("┏━━━━━ "+showName+" Debug [").append(this.getSqlId(formatSql(sqlId))).append("] ━━━").append(lineSeparator)
|
|
|
|
|
// .append("┣ SQL:\t " + formatSql(ctx.getSql()) + lineSeparator)
|
|
|
|
|
// .append("┣ 参数:\t " + formatParas(ctx.getParas())).append(lineSeparator);
|
|
|
|
|
// RuntimeException ex = new RuntimeException();
|
|
|
|
|
// StackTraceElement[] traces = ex.getStackTrace();
|
|
|
|
|
// int index = lookBusinessCodeInTrace(traces);
|
|
|
|
|
// StackTraceElement bussinessCode = traces[index];
|
|
|
|
|
// String className = bussinessCode.getClassName();
|
|
|
|
|
// String mehodName = bussinessCode.getMethodName();
|
|
|
|
|
// int line = bussinessCode.getLineNumber();
|
|
|
|
|
// sb.append("┣ 位置:\t " + className + "." + mehodName + "(" + bussinessCode.getFileName() + ":" + line + ")"
|
|
|
|
|
// + lineSeparator);
|
|
|
|
|
// ctx.put("logs", sb);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// protected int lookBusinessCodeInTrace(StackTraceElement[] traces) {
|
|
|
|
|
//
|
|
|
|
|
// String className = getTraceClassName();
|
|
|
|
|
// for (int i = traces.length - 1; i >= 0; i--) {
|
|
|
|
|
// String name = traces[i].getClassName();
|
|
|
|
|
// if (className != null && className.equals(name)) {
|
|
|
|
|
// return i;
|
|
|
|
|
// } else if (name.equals(mapperName)) {
|
|
|
|
|
// // 越过sun jdk 代理
|
|
|
|
|
// int skipLine = JavaType.isJdk8() ? 3 : 2;
|
|
|
|
|
// return i + skipLine;
|
|
|
|
|
// } else if (name.equals(lambdaQueryName)) {
|
|
|
|
|
// return i + 1;
|
|
|
|
|
// } else if (name.equals(queryClassName)) {
|
|
|
|
|
// return i + 1;
|
|
|
|
|
// } else if (name.equals(sqlManagerName)) {
|
|
|
|
|
// return i + 1;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// // 不可能到这里
|
|
|
|
|
// throw new IllegalStateException();
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// public void after(InterceptorContext ctx) {
|
|
|
|
|
// String sqlId = ctx.getSqlId();
|
|
|
|
|
// if (this.isSimple(sqlId)) {
|
|
|
|
|
// this.simpleOut(ctx);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// long time = System.currentTimeMillis();
|
|
|
|
|
// long start = (Long) ctx.get("debug.time");
|
|
|
|
|
// String lineSeparator = System.getProperty("line.separator", "\n");
|
|
|
|
|
// StringBuilder sb = (StringBuilder) ctx.get("logs");
|
|
|
|
|
// sb.append("┣ 时间:\t " + (time - start) + "ms").append(lineSeparator);
|
|
|
|
|
// if (ctx.isUpdate()) {
|
|
|
|
|
// sb.append("┣ 更新:\t [");
|
|
|
|
|
// if (ctx.getResult().getClass().isArray()) {
|
|
|
|
|
// int[] ret = (int[]) ctx.getResult();
|
|
|
|
|
// for (int i = 0; i < ret.length; i++) {
|
|
|
|
|
// if (i > 0)
|
|
|
|
|
// sb.append(",");
|
|
|
|
|
// sb.append(ret[i]);
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// sb.append(ctx.getResult());
|
|
|
|
|
// }
|
|
|
|
|
// sb.append("]").append(lineSeparator);
|
|
|
|
|
// } else {
|
|
|
|
|
// if (ctx.getResult() instanceof Collection) {
|
|
|
|
|
// sb.append("┣ 结果:\t [").append(((Collection) ctx.getResult()).size()).append("]").append(lineSeparator);
|
|
|
|
|
// } else {
|
|
|
|
|
// sb.append("┣ 结果:\t [").append(ctx.getResult()).append("]").append(lineSeparator);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// sb.append("┗━━━━━ Debug [").append(this.getSqlId(formatSql(ctx.getSqlId()))).append("] ━━━")
|
|
|
|
|
// .append(lineSeparator);
|
|
|
|
|
// println(sb.toString());
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// public String getShowName() {
|
|
|
|
|
// return showName;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// public void setShowName(String showName) {
|
|
|
|
|
// this.showName = showName;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|