xin.xihc.jba.core.utils.JbaLog Maven / Gradle / Ivy
package xin.xihc.jba.core.utils;
import org.slf4j.Logger;
import xin.xihc.jba.core.JbaTemplate;
import xin.xihc.utils.json.JsonUtil;
import xin.xihc.utils.log.LogUtil;
/**
* spring-jba日志记录器
*
* @Author Leo.Xi
* @Date 2019/2/19 15:52
* @Version 1.0
**/
public class JbaLog {
/** 日志文件夹名 */
public static final String JBA_LOG_NAME = "JbaSql";
/** 记录日志 */
private static Logger LOGGER = LogUtil.getLogger(JbaTemplate.class, JBA_LOG_NAME);
/** 慢sql时间限制 */
private static int SLOWLY_SQL_TIME = 400;
private JbaLog() {
}
/**
* 设置慢sql的时间限制
*
* @param slowlySqlTime 时间毫秒
* @author Leo.Xi
* @date 2020/5/26
* @since 0.0.1
*/
public static void setSlowlySqlTime(int slowlySqlTime) {
SLOWLY_SQL_TIME = slowlySqlTime;
}
/**
* 记录SQL语句、执行时间、参数
*
* @param sql sql语句
* @param params sql中的参数
* @param start 开始执行时间戳(到毫秒)
*/
public static void infoSql(final String sql, Object params, final long start) {
long time = System.currentTimeMillis() - start;// 执行时间毫秒(ms)
String log = String.format("【%6s】\t%s\r\n【%6s】\t%s\r\n【%6s】\t%,d(ms)", "sql", sql, "params", JsonUtil
.toNoNullJsonStr(params, false), "time", time);
LOGGER.debug("\r\n" + log);
if (time > SLOWLY_SQL_TIME) { // 慢sql
LOGGER.error("慢SQL:\r\n" + log);
}
}
/**
* 异常
*
* @param e
*/
public static void error(Throwable e) {
LOGGER.error("异常: ", e);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy