Springboot error log is a small tool that intercepts the library, and uses it by yourself

It’s a little troublesome to go to the log to see the error information for the projects I made by myself. It is a little troublesome to write directly to the database and open the database to see it. This is more convenient. It is more suitable for me. This gadget is for your own use, you can improve it if you like it. ------A rookie's narrative

#Table Structure

DROP TABLE IF EXISTS `exception_log`;
CREATE TABLE `exception_log`  (
  `id` varchar(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
  `user_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户',
  `cause` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '异常名字,取30行',
  `message` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '异常内容',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '异常信息' ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

Entity creation


import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 异常信息
 *
 * @author unknown
 * @email [email protected]
 * @date 2020-04-07 15:25:20
 */
@Data
@TableName("exception_log")
public class ExceptionLogEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId
    private String id;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     *
     */
    private String userId;
    /**
     * 异常名字
     */
    private String cause;
    /**
     * 异常内容,取30行
     */
    private String message;

}

Mapper

import com.baomidou.mybatisplus.mapper.BaseMapper;
import io.renren.modules.base.entity.ExceptionLogEntity;
import org.apache.ibatis.annotations.Mapper;

/**
 * 错误日志处理
 *
 * @author liyongfei
 * @email [email protected]
 * @date 2021-05-13 17:31:46
 */
@Mapper
public interface ExceptionLogMapper extends BaseMapper<ExceptionLogEntity> {

}

ExceptionHandler


import io.renren.common.utils.R;
import io.renren.modules.base.dao.ExceptionLogMapper;
import io.renren.modules.base.entity.ExceptionLogEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.annotation.Resource;

/**
 * 异常处理器
 * 
 * @author
 * @email
 * @date 2016年10月27日 下午10:16:19
 */
@RestControllerAdvice
@Slf4j
public class RRExceptionHandler {

	@Resource
	private ExceptionLogMapper exceptionLogMapper;


	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(RRException.class)
	public R handleRRException(RRException e){
		R r = new R();
		r.put("code", e.getCode());
		r.put("msg", e.getMessage());
		ExceptionLogEntity exceptionLogEntity = handlerException(e);
		exceptionLogMapper.insert(exceptionLogEntity);
		return r;
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public R handlerNoFoundException(Exception e) {
		log.error(e.getMessage(), e);
		return R.error(404, "路径不存在,请检查路径是否正确");
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public R handleDuplicateKeyException(DuplicateKeyException e){
		log.error(e.getMessage(), e);
		return R.error("数据库中已存在该记录");
	}

	@ExceptionHandler(AuthorizationException.class)
	public R handleAuthorizationException(AuthorizationException e){
		log.error(e.getMessage(), e);
		return R.error("没有权限,请联系管理员授权");
	}

	@ExceptionHandler(Exception.class)
	public R handleException(Exception e){
		log.error(e.getMessage(), e);
		ExceptionLogEntity exceptionLogEntity = handlerException(e);
		exceptionLogMapper.insert(exceptionLogEntity);
		return R.error();
	}

	private ExceptionLogEntity handlerException(Exception e) {
		ExceptionLogEntity exceptionLogEntity = new ExceptionLogEntity();
		if (e != null) {
			if (e.getCause() != null) {
				if (e.getCause().getMessage().length() > 1000) {
					exceptionLogEntity.setCause(StringUtils.substring(e.getCause().getMessage(), 0, 1000));
				} else {
					exceptionLogEntity.setCause(e.getCause().getMessage());
				}
			}
			if (e.getMessage() != null) {
				if (e.getMessage().length() > 1000) {
					exceptionLogEntity.setMessage(StringUtils.substring(e.getMessage(), 0, 1000));
				} else {
					exceptionLogEntity.setMessage(e.getMessage());
				}
			}

		}
		return exceptionLogEntity;
	}

}
Insert picture description here


Insert picture description here


Insert picture description here


Insert picture description here