springboot(三):统一日志处理

1、统一返回类型

每个接口返回的数据类型一致,方便前端获取数据,新建ResultVo类:

package com.szl.vo;
import com.szl.common.ResultEnum;

/**
 * 接口返回类
 * @author szl
 * @date 2018年6月30日 下午8:07:49
 *
 */
public class ResultVo {

	/** 状态码*/
	private String code;
	/** 提示信息*/
	private String msg;
	/** 返回数据*/
	private Object data;
	
	public static ResultVo get(String code, String msg){
		ResultVo r = new ResultVo();
		r.setCode(code);
		r.setMsg(msg);
		return r;
	}
	
	public static ResultVo get(ResultEnum resultEnum){
		ResultVo r = new ResultVo();
		r.setCode(resultEnum.getCode());
		r.setMsg(resultEnum.getMsg());
		return r;
	}
	
	public static ResultVo getData(ResultEnum resultEnum, Object data){
		ResultVo r = new ResultVo();
		r.setCode(resultEnum.getCode());
		r.setMsg(resultEnum.getMsg());
		r.setData(data);
		return r;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	
}

接口统一返回类型:返回的时候,设置code,msg,data,使返回类型统一

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello(){
    return ResultVo.getData(ResultEnum.SUCCESS, "hello springboot!");
}

2、统一日志处理

通过aop实现执行方法前或执行时和执行完毕打印处理信息:

pom.xml引入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

创建统一处理类:LogAspect.java

package com.szl.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.szl.vo.ResultVo;

/**
 * 日志记录
 * @author szl
 * @date 2018年6月30日 下午5:33:05
 *
 */
@Aspect
@Component
public class LogAspect {

	private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
	
	@Pointcut("execution(* com.szl.controller.*.*(..))")
	public void log(){
		
	}
	
	@Before("log()")
	public void doBefore(JoinPoint joinPoint){
		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		//url
		logger.info("url={}", request.getRequestURI());
		//method
		logger.info("method={}", request.getMethod());
		//ip
		logger.info("ip={}", request.getRemoteAddr());
		//类方法
		logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
		//参数
		logger.info("args={}", joinPoint.getArgs());
	}
	
	@After("log()")
	public void doAfter(){
		
	}
	
	@AfterReturning(pointcut = "log()", returning = "object")
	public void doAfterReturning(Object object){
		if(object instanceof ResultVo){
			ResultVo resultVo = (ResultVo)object;
			if(resultVo.getData() != null){
				logger.info("response={}", resultVo.getData().toString());
			}
		}else{
			logger.info("response={}", object);
		}
	}
	
	 @Around("log()")
	 public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
         long time = System.currentTimeMillis();
	     Object retVal = null;
		  try {
		   	retVal = pjp.proceed();
		  } finally {
			 time = System.currentTimeMillis() - time;
			 logger.info(String.format("process time: %d ms @%s.%s", time,     pjp.getTarget().getClass().getName(),
			pjp.getSignature().getName()));
	    }
	    return retVal;
	 }

}

@Pointcut:切入点

@Before:在执行切入点方法之前执行

@After:在执行完切入点方法之后执行

@AfterReturning:获取执行完成返回的信息

@Around:执行前后,pjp.proceed()方法执行,result为返回的数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值