1. 程式人生 > >springboot學習入門簡易版六---springboot2.0整合全局捕獲異常及log4j日誌(12-13)

springboot學習入門簡易版六---springboot2.0整合全局捕獲異常及log4j日誌(12-13)

final 發生 family actor 頁面 layout lan param rfi

使用Aop實現

1創建異常請求

在原有項目基礎上,jspController中創建一個可能發生異常的請求:

/**

* 全局捕獲異常測試

* @param i

* @return

*/

@RequestMapping("/testGloableException")

@ResponseBody

public String testGloableException(int i) {

int j=2/i;

return "test";

}

2創建全局捕獲異常處理類

/**

* 全局捕獲異常

* @author admin

*

*/

@ControllerAdvice(basePackages="com.springbootjsp")

public class GloableExceptionHandler {

/**

* 使用全局捕獲異常不建議將異常吃掉

* @return

*/

@ExceptionHandler(RuntimeException.class)

@ResponseBody //返回json格式

public Map<String,Object> errorResult(){

Map<String,Object> map=new HashMap<>();

map.put("errorCode", "error_500");

map.put("errorMsg", "全局捕獲異常");

return map;

}

}

3 啟動項目,並訪問:

http://localhost:8080/testGloableException?i=0

頁面返回:{"errorCode":"error_500","errorMsg":"全局捕獲異常"}

2.8 SpringBoot整合log4j日誌記錄(13

1引入log4j包

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-log4j2</artifactId>

</dependency>

2 log4j配置

resources下創建log4j.properties文件

# LOG4J\u914D\u7F6E

log4j.rootCategory=INFO, stdout, file

# \u63A7\u5236\u53F0\u8F93\u51FA

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

# root\u65E5\u5FD7\u8F93\u51FA\u5230\u6587\u4EF6

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender

log4j.appender.file.file=/logsdata/logs/springboot-log4j-all.log

log4j.appender.file.DatePattern=‘.‘yyyy-MM-dd

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

# \u6309\u4E0D\u540Cpackage\u8FDB\u884C\u8F93\u51FA

# com.micai\u5305\u4E0B\u7684\u65E5\u5FD7\u914D\u7F6E

log4j.category.com.micai=DEBUG, didifile

# com.micai\u4E0B\u7684\u65E5\u5FD7\u8F93\u51FA

log4j.appender.didifile=org.apache.log4j.DailyRollingFileAppender

log4j.appender.didifile.file=/logsdata/logs/springboot-log4j-my.log

log4j.appender.didifile.DatePattern=‘.‘yyyy-MM-dd

log4j.appender.didifile.layout=org.apache.log4j.PatternLayout

log4j.appender.didifile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L ---- %m%n

# ERROR\u7EA7\u522B\u8F93\u51FA\u5230\u7279\u5B9A\u7684\u65E5\u5FD7\u6587\u4EF6\u4E2D

log4j.logger.error=errorfile

# error\u65E5\u5FD7\u8F93\u51FA

log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender

log4j.appender.errorfile.file=/logsdata/logs/springboot-log4j-error.log

log4j.appender.errorfile.DatePattern=‘.‘yyyy-MM-dd

log4j.appender.errorfile.Threshold = ERROR

log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout

log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

3 使用

@Controller

public class JspController {

public static final Logger log=LoggerFactory.getLogger(JspController.class);

@RequestMapping("/testGloableException")

@ResponseBody

public String testGloableException(int i) {

log.info("testGloableException====");

int j=2/i;

return "test";

}

2.9 SpringBoot使用aop統一處理web請求日誌(14

1pom配置

<!-- spring-boot-starter-parent 整合第三方常用框架依賴信息(包含各種依賴信息) -->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.0.RELEASE</version>

</parent>

<!-- spring-boot-starter-web springboot整合springmvc web

實現原理:maven依賴繼承關系,相當於把第三方常用maven依賴信息,在parent項目中已封裝

-->

<dependencies>

<!-- 根據需要選擇parent中封裝的第三方框架 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<!-- 不需要寫版本號,因為在parent中已封裝好版本號 -->

</dependency>

<!-- log4j2 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-log4j2</artifactId>

</dependency>

<!-- aop -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-aop</artifactId>

</dependency>

</dependencies>

2創建切面

WebLogAspect類

@Aspect

@Component

public class WebLogAspect {

private final static Logger logger=LoggerFactory.getLogger(WebLogAspect.class);

@Pointcut("execution(public * com.springboot2..*.*(..))")

public void weblog() {

}

@Before("weblog()")

public void doBefore(JoinPoint joinPoint) {

//接收到請求,記錄請求內容

ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request=attributes.getRequest();

//記錄下請求內容

logger.info("URL:"+ request.getRequestURI().toLowerCase());

logger.info("HTTP_METHOD:"+request.getMethod());

logger.info("IP:"+request.getRemoteAddr());

Enumeration<String> enu=request.getParameterNames();

while (enu.hasMoreElements()) {

String name=(String)enu.nextElement();

logger.info("name:{},value:{}",name,request.getParameter(name));

}

}

@AfterReturning(returning="obj" ,pointcut="weblog()")

public void deAfter(Object obj) {

logger.info("Response:"+obj);

}

項目結構:

技術分享圖片

3創建controller

@RestController

public class SecondController {

@RequestMapping("/second")

public String second() {

return "second test";

}

}

4 訪問http://localhost:8080/second

可看到後臺日誌輸出

技術分享圖片

5說明:

Aop表達式

https://blog.csdn.net/somilong/article/details/74568223

git代碼:https://github.com/cslj2013/springboot2.0_log_aop.git

springboot學習入門簡易版六---springboot2.0整合全局捕獲異常及log4j日誌(12-13)