添加日志工具

This commit is contained in:
zhaoyong 2021-08-23 18:51:52 +08:00
parent a18d1b0c30
commit ba818d5151
2 changed files with 134 additions and 4 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.axzo.framework</groupId>
<artifactId>common-common</artifactId>
<version>1.0.10</version>
<version>1.0.11</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -19,6 +19,7 @@
<lombok.version>1.18.18</lombok.version>
<commons-codec.version>1.8</commons-codec.version>
<slf4j-api.version>1.7.5</slf4j-api.version>
<mybatis-plus.version>3.3.2</mybatis-plus.version>
<lombok.version>1.18.18</lombok.version>
<fastjson.version>1.2.47</fastjson.version>
<joda-time.version>2.10.6</joda-time.version>
@ -26,7 +27,7 @@
<jackson-databind.version>2.10.2</jackson-databind.version>
<apm-toolkit-trace.version>6.5.0</apm-toolkit-trace.version>
<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
<spring-context.version>4.3.19.RELEASE</spring-context.version>
<spring.version>4.3.19.RELEASE</spring.version>
<javax.validation.version>2.0.0.Final</javax.validation.version>
<hibernate.validator.version>6.0.16.Final</hibernate.validator.version>
</properties>
@ -84,11 +85,24 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool-all.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>${mybatis-plus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-context.version}</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -112,7 +126,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-context.version}</version>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -0,0 +1,116 @@
package cn.azxo.framework.common.utils;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.IbatisException;
import org.slf4j.MDC;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.dao.DataAccessException;
import java.sql.SQLException;
/**
* 日志打印工具
*
* @author zhaoyong
* @see LogUtil
* @since 2021-08-23 18:31
*/
@Slf4j
public class LogUtil {
private static final String TYPE_KEY = "errorType";
private static final String LEVEL_KEY = "errorLevel";
public static void error(String message, Object... params) {
error(ErrorType.ERROR_BUSINESS, message, params);
}
public static void error(ErrorType type, String message, Object... params) {
error(ErrorLevel.P0, type, message, params);
}
public static void error(ErrorLevel level, ErrorType type, String message, Object... params) {
if (level == null) {
level = ErrorLevel.P0;
}
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(message, params);
Throwable throwable = formattingTuple.getThrowable();
//检查是否是sql异常
type = checkType(type, throwable);
message = formattingTuple.getMessage();
MDC.put(TYPE_KEY, type.value);
MDC.put(LEVEL_KEY, level.value);
try {
log.error(message, throwable);
} finally {
MDC.remove(TYPE_KEY);
MDC.remove(LEVEL_KEY);
}
}
/**
* 根据参数检查是否是sql异常
*
* @param originType 原始类型
* @param p 异常
* @return 检测后的error类型
*/
private static ErrorType checkType(ErrorType originType, Throwable p) {
if (originType == null) {
return ErrorType.ERROR_UNKNOWN;
}
if (isSqlException(p)) {
return ErrorType.ERROR_SQL;
} else {
return originType;
}
}
private static boolean isSqlException(Object p) {
if (p == null) {
return false;
}
return p instanceof SQLException
|| p instanceof IbatisException
|| p instanceof DataAccessException
|| p instanceof MybatisPlusException;
}
public enum ErrorType {
ERROR_SYSTEM("系统异常"),
ERROR_BUSINESS("业务异常"),
ERROR_SQL("Sql异常"),
ERROR_INNER_SERVICE("服务间调用异常"),
ERROR_THIRD_SERVICE("第三方服务异常"),
ERROR_UNKNOWN("未知异常");
private final String value;
ErrorType(String value) {
this.value = value;
}
}
public enum ErrorLevel {
P0("P0"),
P1("P1"),
P2("P2"),
P3("P3"),
P4("P4");
private final String value;
ErrorLevel(String value) {
this.value = value;
}
}
}