日志在开发中必不可少,方便记录代码执行过程、排查问题。下面是两种日志的使用
1、日志切面:通过切面记录请求参数和响应结果
2、log日志:记录正常信息(log.info),错误信息(log.error、log.warn)
1、日志切面
package com.xxx.datamanager.aop; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; 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.Field; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; /** * @author * @说明:日志切面 * @create */ @Aspect @Component @Slf4j public class AccessLog { @Pointcut("execution(public * com..xxx.datamanager.controller.*.*(..))") public void log() { } @Around("log()") public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { long start = System.currentTimeMillis(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Map<String, String> requestInfo = new HashMap<>(); requestInfo.put("remoteIp", request.getRemoteAddr()); requestInfo.put("httpMethod", request.getMethod()); requestInfo.put("requestUrl", request.getRequestURL().toString()); Object[] args = proceedingJoinPoint.getArgs(); Map<String, String[]> parameterMap = request.getParameterMap(); if (parameterMap.size() == 0 && args.length > 0) { requestInfo.put("requestParam", JSONObject.valueToString(getKeyAndValue(args[0]))); } else { requestInfo.put("requestParam", JSONObject.valueToString(parameterMap)); } log.info(LogFormat.build().putTags(requestInfo).message("请求信息")); Map<String, String> responseInfo = new HashMap<>(); Object result = proceedingJoinPoint.proceed(); responseInfo.put("response", new JSONObject(result).toString()); responseInfo.put("timeCost", (System.currentTimeMillis() - start) + "ms"); log.info(LogFormat.build().putTags(responseInfo).message("响应信息")); return result; } public static Map<String, Object> getKeyAndValue(Object object) throws IllegalAccessException { Map<String, Object> parameMap = new HashMap<>(); if (null == object) return parameMap; Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field.get(object) == null) continue; parameMap.put(field.getName(), field.get(object)); } return parameMap; } }
2、log.info/log.error——@Slf4j
log.info、log.error、log.warn 中支持占位符,如log.info("打印 {},{}", v1, v2);
当只有一个参数时,占位符不生效(只要将异常信息作为最后一个参数即可,不论使用还是不使用占位符,都不会影响异常信息的输出,只不过占位符不会生效)
// log.info:用于记录查库信息 / rpc调用结果 / 需要打印的重要信息,在方法中直接使用log.info List<Integer> datasetIds = datasetAccessMapper.selectByMisId(misId, startFrom, size); log.info(">>>> datasetIds: {}", JsonUtils.toJson(datasetIds));
//log.error:用于记录报错信息,可用于try catch异常中/全局异常处理 package com.xxx.exception; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @Slf4j @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) private ResponseEntity<?> handleUnknownException(Exception e) { log.error("服务异常:", e); //日志记录 return ResponseEntity.status(HttpStatus.OK).body(Response.error(INTERNAL_ERROR)); //返回结果 } }
标签:info,log,org,put,import,日志 From: https://www.cnblogs.com/zhegemaw/p/18338840