Files
hutool/hutool-db/src/main/java/cn/hutool/db/sql/SqlLog.java
2023-03-27 02:02:10 +08:00

91 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.hutool.db.sql;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
/**
* SQL在日志中打印配置
*
* @author looly
* @since 4.1.0
*/
public enum SqlLog {
/**
* 单例
*/
INSTANCE;
private final static Log log = LogFactory.get();
/**
* 是否debugSQL
*/
private boolean showSql;
/**
* 是否格式化SQL
*/
private boolean formatSql;
/**
* 是否显示参数
*/
private boolean showParams;
/**
* 默认日志级别
*/
private Level level = Level.DEBUG;
/**
* 设置全局配置是否通过debug日志显示SQL
*
* @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL
* @param isShowParams 是否打印参数
* @param level 日志级别
*/
public void init(final boolean isShowSql, final boolean isFormatSql, final boolean isShowParams, final Level level) {
this.showSql = isShowSql;
this.formatSql = isFormatSql;
this.showParams = isShowParams;
this.level = level;
}
/**
* 打印SQL日志
*
* @param sql SQL语句
* @since 4.6.7
*/
public void log(final String sql) {
log(sql, null);
}
/**
* 打印批量 SQL日志
*
* @param sql SQL语句
* @since 4.6.7
*/
public void logForBatch(final String sql) {
if (this.showSql) {
log.log(this.level, "\n[Batch SQL] -> {}", this.formatSql ? SqlFormatter.format(sql) : sql);
}
}
/**
* 打印SQL日志
*
* @param sql SQL语句
* @param paramValues 参数可为null
*/
public void log(final String sql, final Object paramValues) {
if (this.showSql) {
if (null != paramValues && this.showParams) {
log.log(this.level, "\n[SQL] -> {}\nParams -> {}", this.formatSql ? SqlFormatter.format(sql) : sql, paramValues);
} else {
log.log(this.level, "\n[SQL] -> {}", this.formatSql ? SqlFormatter.format(sql) : sql);
}
}
}
}