使用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文件
# LOG4Ju914Du7F6E
log4j.rootCategory=INFO, stdout, file
# u63A7u5236u53F0u8F93u51FA
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
# rootu65E5u5FD7u8F93u51FAu5230u6587u4EF6
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
# u6309u4E0Du540Cpackageu8FDBu884Cu8F93u51FA
# com.micaiu5305u4E0Bu7684u65E5u5FD7u914Du7F6E
log4j.category.com.micai=DEBUG, didifile
# com.micaiu4E0Bu7684u65E5u5FD7u8F93u51FA
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
# ERRORu7EA7u522Bu8F93u51FAu5230u7279u5B9Au7684u65E5u5FD7u6587u4EF6u4E2D
log4j.logger.error=errorfile
# erroru65E5u5FD7u8F93u51FA
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表达式