打印慢sql日志

This commit is contained in:
Redkale
2022-12-22 21:49:11 +08:00
parent 2d06fd6ea9
commit 989f1cddf8
4 changed files with 56 additions and 121 deletions

View File

@@ -16,6 +16,7 @@ import org.redkale.annotation.*;
import org.redkale.annotation.ResourceListener;
import static org.redkale.boot.Application.*;
import org.redkale.boot.*;
import static org.redkale.boot.Application.*;
import org.redkale.convert.ConvertDisabled;
import org.redkale.convert.json.JsonConvert;
import org.redkale.mq.MessageMultiConsumer;
@@ -58,6 +59,8 @@ public abstract class ClusterAgent {
protected AnyValue config;
protected Set<String> tags;
protected TransportFactory transportFactory;
protected final ConcurrentHashMap<String, ClusterEntry> localEntrys = new ConcurrentHashMap<>();
@@ -81,7 +84,18 @@ public abstract class ClusterAgent {
if (str.trim().isEmpty()) continue;
list.add(Integer.parseInt(str.trim()));
}
if (!list.isEmpty()) this.ports = list.stream().mapToInt(x -> x).toArray();
if (!list.isEmpty()) {
this.ports = list.stream().mapToInt(x -> x).toArray();
}
}
Set<String> tags0 = new HashSet<>();
for (String str : config.getValue("tags", "").split(";|,")) {
if (!str.trim().isEmpty()) {
tags0.add(str.trim());
}
}
if (!tags0.isEmpty()) {
this.tags = tags0;
}
}

View File

@@ -69,8 +69,11 @@ public abstract class AbstractDataSource extends AbstractService implements Data
//@since 2.7.0
public static final String DATA_SOURCE_PIPELINES = "pipelines";
//@since 2.7.0 //超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 3000
public static final String DATA_SOURCE_SLOWMS = "slowms";
//@since 2.8.0 //超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 2000
public static final String DATA_SOURCE_SLOWMS_WARN = "warnslowms";
//@since 2.8.0 //超过多少毫秒视为较慢, 会打印警告级别的日志, 默认值: 3000
public static final String DATA_SOURCE_SLOWMS_ERROR = "errorslowms";
//@since 2.7.0
public static final String DATA_SOURCE_AUTOMAPPING = "auto-mapping";

View File

@@ -271,13 +271,8 @@ public class DataJdbcSource extends DataSqlSource {
String debugsql = sb.toString();
if (info.isLoggable(logger, Level.FINEST, debugsql)) logger.finest(info.getType().getSimpleName() + " insert sql=" + debugsql.replaceAll("(\r|\n)", "\\n"));
}
} //打印结束
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
} //打印结束
slowLog(s, sql);
return CompletableFuture.completedFuture(c);
} catch (SQLException e) {
return CompletableFuture.failedFuture(e);
@@ -332,12 +327,7 @@ public class DataJdbcSource extends DataSqlSource {
final Statement stmt = conn.createStatement();
int c = stmt.executeUpdate(sql);
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(c);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -355,12 +345,7 @@ public class DataJdbcSource extends DataSqlSource {
st.executeBatch();
}
st.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(0);
} catch (SQLException e2) {
return CompletableFuture.failedFuture(e2);
@@ -385,12 +370,7 @@ public class DataJdbcSource extends DataSqlSource {
final Statement stmt = conn.createStatement();
int c = stmt.executeUpdate(sql);
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(c);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) return CompletableFuture.completedFuture(-1);
@@ -415,12 +395,7 @@ public class DataJdbcSource extends DataSqlSource {
String tablekey = table.indexOf('.') > 0 ? table : (conn.getCatalog() + '.' + table);
info.removeDisTable(tablekey);
}
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(c);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) return CompletableFuture.completedFuture(-1);
@@ -474,12 +449,7 @@ public class DataJdbcSource extends DataSqlSource {
if (p >= 0) c += p;
}
prestmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + updateSQL);
}
}
slowLog(s, updateSQL);
return CompletableFuture.completedFuture(c);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -527,24 +497,14 @@ public class DataJdbcSource extends DataSqlSource {
}
int c = prestmt.executeUpdate();
prestmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(c);
} else {
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement();
int c = stmt.executeUpdate(sql);
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(c);
}
} catch (SQLException e) {
@@ -598,12 +558,7 @@ public class DataJdbcSource extends DataSqlSource {
}
set.close();
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(map);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -649,12 +604,7 @@ public class DataJdbcSource extends DataSqlSource {
}
set.close();
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(rs);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -701,12 +651,7 @@ public class DataJdbcSource extends DataSqlSource {
}
set.close();
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(rs);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -768,12 +713,7 @@ public class DataJdbcSource extends DataSqlSource {
}
set.close();
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(rs);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -816,12 +756,7 @@ public class DataJdbcSource extends DataSqlSource {
T rs = set.next() ? selects == null ? info.getFullEntityValue(set) : info.getEntityValue(selects, set) : null;
set.close();
ps.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(rs);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -868,12 +803,7 @@ public class DataJdbcSource extends DataSqlSource {
}
set.close();
ps.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(val == null ? defValue : val);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -916,12 +846,7 @@ public class DataJdbcSource extends DataSqlSource {
set.close();
ps.close();
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " exists (" + rs + ") sql=" + sql);
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return CompletableFuture.completedFuture(rs);
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -990,12 +915,7 @@ public class DataJdbcSource extends DataSqlSource {
set.close();
ps.close();
}
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, sheet-content: " + listsql);
}
}
slowLog(s, listsql);
return CompletableFuture.completedFuture(new Sheet<>(total, list));
}
final String listsql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getFullQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join)
@@ -1031,12 +951,7 @@ public class DataJdbcSource extends DataSqlSource {
}
set.close();
ps.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + listsql);
}
}
slowLog(s, listsql);
return CompletableFuture.completedFuture(new Sheet<>(total, list));
} catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) {
@@ -1103,12 +1018,7 @@ public class DataJdbcSource extends DataSqlSource {
rs[++i] = stmt.execute(sql) ? 1 : 0;
}
stmt.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls));
}
}
slowLog(s, sqls);
return rs;
} catch (SQLException e) {
throw new RuntimeException(e);
@@ -1141,12 +1051,7 @@ public class DataJdbcSource extends DataSqlSource {
V rs = handler.apply(createDataResultSet(null, set));
set.close();
statement.close();
if (slowms > 0) {
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
slowLog(s, sql);
return rs;
} catch (Exception ex) {
throw new RuntimeException(ex);

View File

@@ -71,8 +71,11 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
protected final BiFunction<DataSource, EntityInfo, CompletableFuture<List>> fullloader = (s, i)
-> ((CompletableFuture<Sheet>) querySheetDB(i, false, false, false, null, null, (FilterNode) null)).thenApply(e -> e == null ? new ArrayList() : e.list(true));
//超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 3000
protected long slowms;
//超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 2000
protected long slowmsWarn;
//超过多少毫秒视为很慢, 会打印错误级别的日志, 默认值: 3000
protected long slowmsError;
//用于反向LIKE使用
protected String containSQL;
@@ -127,7 +130,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
this.tablecopySQL = readConfProps.getProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, "CREATE TABLE IF NOT EXISTS ${newtable} LIKE ${oldtable}");
this.cacheForbidden = "NONE".equalsIgnoreCase(readConfProps.getProperty(DATA_SOURCE_CACHEMODE));
this.slowms = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS, "3000").trim());
this.slowmsWarn = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS_WARN, "2000").trim());
this.slowmsError = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS_ERROR, "3000").trim());
}
@Override
@@ -216,6 +220,15 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
throw new UnsupportedOperationException("Not supported yet.");
}
protected void slowLog(long startTime, String... sqls) {
long cost = System.currentTimeMillis() - startTime;
if (slowmsError > 0 && cost > slowmsError) {
logger.log(Level.SEVERE, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls));
} else if (slowmsWarn > 0 && cost > slowmsWarn) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') very slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls));
}
}
//解密可能存在的加密字段, 可重载
protected String decryptProperty(String key, String value) {
return value;