1.Log输出 slf4j
package com.followme.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.Util;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* Created by 2019-04-20.
*/
public class LogUtil {
private static final Map<Class<?>,Logger> loggers = new ConcurrentHashMap<>();
// info 级别日志
public static void info(String message, Object... objects){
Class<?> callingClass = Util.getCallingClass();
Logger logger =loggers.computeIfAbsent(callingClass, LoggerFactory::getLogger);
List<Object> objectList = Arrays.stream(objects).collect(Collectors.toList());
Object[] objectArr = new Object[objectList.size()];
logger.info(message,objectList.toArray(objectArr));
}
// debug 级别日志
public static void debug(String message, Object... objects){
Class<?> callingClass = Util.getCallingClass();
Logger logger =loggers.computeIfAbsent(callingClass, LoggerFactory::getLogger);
List<Object> objectList = Arrays.stream(objects).collect(Collectors.toList());
Object[] objectArr = new Object[objectList.size()];
logger.debug(message,objectList.toArray(objectArr));
}
// error 级别日志
public static void error(String message, Object... objects){
Class<?> callingClass = Util.getCallingClass();
Logger logger =loggers.computeIfAbsent(callingClass, LoggerFactory::getLogger);
List<Object> objectList = Arrays.stream(objects).collect(Collectors.toList());
Object[] objectArr = new Object[objectList.size()];
logger.error(message,objectList.toArray(objectArr));
}
}
2.AOP
package com.followme.common.aspect;
import com.alibaba.fastjson.JSON;
import com.followme.common.utils.LogUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* Created by 2019-04-20.
*/
@Aspect
@Component
public class ControllerLogAspect {
@Pointcut("execution(* com.followme.controller..*(..))")
public void log() {
}
// 请求入参打印
@Before("log()")
public void doBefore(JoinPoint joinPoint) throws Exception {
LogUtil.info(joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName() + "启动:请求数据为:{}",
this.buildArgs(joinPoint.getArgs()));
}
// 返回结果打印
@AfterReturning(returning = "object",pointcut = "log()")
public void doAfterReturning(JoinPoint joinPoint,Object object) throws Exception {
LogUtil.info(joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName() + "结束:响应数据为:{}",
this.buildArgs(joinPoint.getArgs()));
}
// 异常打印
@AfterThrowing(throwing = "e",pointcut = "log()")
public void doAfterThrowing(JoinPoint joinPoint,Exception e) throws Exception {
LogUtil.info(joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName() + "结束:响应异常为:{}:{}",
e.getClass(),e.getMessage());
}
// 获取参数
private String buildArgs(Object[] objects) {
StringBuffer result = new StringBuffer();
for (Object obj : objects) {
result.append(JSON.toJSONString(obj));
}
return result.toString();
}
}
···