Spring boot global exception captured by the day + log

First, realize the function

    1, a user request return result ajax unified package;

    2, the system automatically generates a log file by day, periodic automatic processing;

    3, the system automatically captures abnormal and abnormality information (including stack information) is output to the automatic log;

    4, custom service exceptions, some of improper behavior by a user to log records, and timely feedback to the page.

Two, ajax results returned package

package com.example.testproject.commom;

import java.util.LinkedHashMap;

/**
 * ajax请求后需要接受的JSON
 * 统一返回值类型,便于在页面上进行统一处理
 */
public class AjaxJson {

	private boolean success = true;    // 是否成功
	private String errorCode = "-1";   //错误代码
	private String msg = "操作成功";    // 提示信息
	private LinkedHashMap<String, Object> body = new LinkedHashMap();//封装json的map
	
	public AjaxJson() {
	}

	/**
	 * 出现异常后调用此方法封装异常
	 * @param e 异常
	 */
	public AjaxJson(Throwable e) {
		this.success = false;
		this.errorCode = "-1";
		this.msg = e.getMessage();
	}

	public LinkedHashMap<String, Object> getBody() {
		return body;
	}

	public void setBody(LinkedHashMap<String, Object> body) {
		this.body = body;
	}

	public void put(String key, Object value){//向json中添加属性,在js中访问,请调用data.body.key
		body.put(key, value);
	}
	
	public void remove(String key){
		body.remove(key);
	}
	
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {//向json中添加属性,在js中访问,请调用data.msg
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getErrorCode() {
		return errorCode;
	}
}

Third, custom exception class service

package com.example.testproject.exception;

/**
 * 自定义业务异常类
 */
public class ServiceException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public ServiceException() {
		super();
	}

	public ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}

	public ServiceException(String message, Throwable cause) {
		super(message, cause);
	}

	public ServiceException(String message) {
		super(message);
	}

	public ServiceException(Throwable cause) {
		super(cause);
	}

}

Fourth, the global exception class capture 

package com.example.testproject.exception;

import com.example.testproject.commom.AjaxJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常捕获类
 */
// 过此注解声明此类为一个全局异常处理类
@ControllerAdvice
public class GlobalExceptionHandler {
	
	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	@ExceptionHandler(Exception.class)
	public void handleException(HttpServletRequest request, Exception e) {
		logger.error("---------------------- handleException start ---------------------- ");
		logger.error("url: [{}]", request.getRequestURL().toString());
		logger.error(e.getMessage(), e);
		logger.error("---------------------- handleException end ---------------------- ");
	}

	@ExceptionHandler(ServiceException.class)
	@ResponseBody
	public AjaxJson handleServiceException(HttpServletRequest request, ServiceException e) {
		logger.error("---------------------- handleServiceException start ---------------------- ");
		logger.error("url: [{}]", request.getRequestURL().toString());
		logger.error(e.getMessage(), e);
		logger.error("---------------------- handleServiceException end ---------------------- ");
		return new AjaxJson(e);
	}

}

V. custom logging configuration (in days output)

    Add logback-spring.xml at src / main / resources configuration log file, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback</contextName>
    <!-- 定义保存日志文件路径 -->
    <property name="log.path" value="./logs/" />
    <!-- 输出到控制台 -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - [%p] - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 输出到文件 -->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}logback.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - [%p] - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>

    <!-- 打印执行的name指定所在的包,additivity设定为true时,父级也会打印相应的信息,相当于打印多次 -->
    <logger name="org.springframework.web" level="ERROR" additivity="false">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </logger>
    <!--由于Dao层使用的是jdbcTemplate,需要单独定义打印执行的SQL-->
    <logger name="org.springframework.jdbc.core" level="DEBUG" additivity="false">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </logger>
    <!--打印系统业务日志-->
    <logger name="com.example" level="DEBUG" additivity="false">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </logger>

</configuration>

Note: The log configuration unfamiliar or want to know more about the students, you can see "SpringBoot learning - (X) SpringBoot log processing" of this blog, written in great detail.

 Sixth, and test results

    6.1, the test class

@Controller
public class TestExceptionController {

    @RequestMapping("testException")
    @ResponseBody
    public AjaxJson index(String type){
        AjaxJson result = new AjaxJson();
        if("serviceE".equals(type)){
            throw new ServiceException("由于用户行为不当造成的异常!");
        } else {
            type = null;
            type.indexOf(",");
        }
        return result;
    }

}

     6.2, Service Exception

     6.2.1, page get an error message

Service Exception pages receive data

    6.2.2, the log file records detailed error information

Log Files

 

Published 47 original articles · won praise 16 · views 70000 +

Guess you like

Origin blog.csdn.net/zorro_jin/article/details/95892166