廈門排名推廣杭州百度首頁優(yōu)化
springboot通過aop自定義注解@Log實現(xiàn)日志打印
文章目錄
- 效果圖
- 實操步驟
- 1.引入依賴
- 2.自定義日志注解
- 3.編寫日志切面類
- 4.UserController
- 5.運行
效果圖
實操步驟
注意,本代碼在springboot環(huán)境下運行,jdk1.8
1.引入依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency><groupId>org.jodd</groupId><artifactId>jodd</artifactId><version>3.3.7</version>
</dependency>
2.自定義日志注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 日志注解* @author woniu*/
@Retention(RetentionPolicy.RUNTIME) //注解在源碼、字節(jié)碼、運行期間都存在
@Target({ElementType.METHOD}) //作用在方法上
public @interface WoniuLog {
}
3.編寫日志切面類
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
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 javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Optional;import static jodd.util.MimeTypes.MIME_APPLICATION_JSON;/*** 日志切面類* @author woniu*/
@Aspect //代表這是一個切面類
@Component //注入到spring ioc
public class WoniuLogAspect {private static final Logger log = LoggerFactory.getLogger(WoniuLogAspect.class);public WoniuLogAspect() {}/*** 前置通知:* @annotation(WoniuLog) 表示切面只對加了@WoniuLog的方法生效*/@Before("@annotation(WoniuLog)")public void doBefore(JoinPoint joinPoint) {ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (requestAttributes != null) {HttpServletRequest request = requestAttributes.getRequest();String contentType = request.getContentType();//只有contentType=application/json 的才加日志打印if (StringUtils.isNotEmpty(contentType)&&contentType.contains(MIME_APPLICATION_JSON)) {Class<?> clazz = joinPoint.getTarget().getClass();Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();log.info("------------------------------------------AOP日志start--------------------------------------------------------");log.info("[AOP日志]:類名:{}", clazz.getName());log.info("[AOP日志]:方法名:{}", method.getName());Optional.ofNullable(joinPoint.getArgs()).ifPresent(x -> {for (Object arg : x) {String temp = JSONArray.toJSONString(x);log.info("[AOP日志]:方法入?yún)?{}", temp);}});log.info("------------------------------------------AOP日志end--------------------------------------------------------");}}}}
4.UserController
@ApiOperation(value = "查詢分頁列表")
@PostMapping("/pageList")
@WoniuLog
public Result<PageResult<UserRespVo>> pageList(@RequestBody UserReqVo reqVo) {PageResult<UserRespVo> result = userService.findList(reqVo);return Result.ok(result);
}