?以上來(lái)自博客:http://tiankong6622.iteye.com/blog/2092733
日志實(shí)體類
import lombok.Data;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * @Author: jj * @Date: 2019/7/30 13:58 * @Version 1.0 */@Datapublic class Logs { private String methodName; private String className; private List<Params> params = new ArrayList<>(); private Object returnData; private String description; private String comments; private Date actionTime; private String userName; private String ip; @Data class Params { private String paramsName; private String typeName; private String paramsDescription; private Object value; } public String printLogs() { String par = ""; for (Params param : params) { par = par " ;{" param.paramsName " ( " param.typeName " / " param.paramsDescription " ) : " param.value "}"; } return "\t\t\t Time: " new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(actionTime) "=============" className " { " methodName " ( " description " ) ---> < " par.substring(2) " > return: " returnData " } " "\t\t\t ****用戶:" userName " IP:" ip " 操作:" comments "****"; }}
?日志注解
import java.lang.annotation.*;/** * @Author: jj * @Date: 2019/7/30 13:21 * @Version 1.0 */@Documented@Target(ElementType.METHOD)@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface LoggingInfo { String methodDescription() default ""; Param[] paramsDescription(); String comments();}
參數(shù)注釋注解
import java.lang.annotation.*;/** * @Author: jj * @Date: 2019/7/30 15:13 * @Version 1.0 */@Documented@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE})@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface Param { String param(); String description() default "";}
切面類
import com.agrant.biz.commons.exception.ServiceException;import com.agrant.biz.commons.utils.ShiroSessionUtils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;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 javax.servlet.http.HttpSession;import java.lang.reflect.Method;import java.lang.reflect.Parameter;import java.util.Date;/** * @Author: jj * @Date: 2019/7/30 13:19 * @Version 1.0 */@Aspect@Componentpublic class LoggingAspect { //本地異常日志記錄對(duì)象 private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class); Logs log = null; //Controller層切點(diǎn) @Pointcut("@annotation(com.agrant.biz.service.aspect.log.LoggingInfo)") public void controllerAspect() { } /** * * @param joinPoint 切點(diǎn) */ @Before("controllerAspect()") public void doBefore(JoinPoint joinPoint) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); HttpSession session = request.getSession(); //讀取session中的用戶 String currentUser = ShiroSessionUtils.getUser().get("name").toString(); //請(qǐng)求的IP String ip = request.getRemoteAddr(); //獲取類名 String targetName = joinPoint.getTarget().getClass().getName(); //獲取方法名 String methodName = joinPoint.getSignature().getName(); Method currentMethod = ((MethodSignature) joinPoint.getSignature()).getMethod(); //*========數(shù)據(jù)庫(kù)日志=========*// log = new Logs(); log.setActionTime(new Date(System.currentTimeMillis())); log.setUserName(currentUser); log.setIp(ip); log.setClassName(targetName); log.setMethodName(methodName); try { Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { method.getGenericReturnType(); Parameter[] parameters = method.getParameters(); log.setComments(method.getAnnotation(LoggingInfo.class).comments()); log.setDescription(method.getAnnotation(LoggingInfo.class).methodDescription()); Param[] parametersDescription = method.getAnnotation(LoggingInfo.class).paramsDescription(); if (parameters.length != parametersDescription.length) { throw new ServiceException("LoggingAspect注解參數(shù)長(zhǎng)度有誤??!"); } for (int i = 0; i < parameters.length; i ) { Parameter parameter = parameters[i]; Param param = parametersDescription[i]; System.out.println("param" param.param() "description" param.description()); Logs.Params paramLog = log.new Params(); paramLog.setParamsName(param.param()); paramLog.setTypeName(parameter.getType().getTypeName()); paramLog.setParamsDescription(param.description()); paramLog.setValue(arguments[i]); log.getParams().add(paramLog); } break; } } } } catch (ClassNotFoundException e) { logger.error(ServiceException.collectExceptionStackMsg(e)); } } @AfterReturning(pointcut = "controllerAspect()", returning = "ret") public Object doAfter(JoinPoint joinPoint, Object ret) { log.setReturnData(ret); logger.info(log.printLogs());// loggingMapper.insert(); return ret; }}
使用實(shí)例
日志打印結(jié)果:
Time: 2019-07-30 19:28:28=============com.agrant.biz.service.imp.BiReportServiceImpl { enteredListing ( 消耗信息輸入頁(yè) ) ---> < {mediaId ( java.lang.Integer / 媒體id ) : 1} ;{agentMonth ( java.lang.String / 代理商數(shù)據(jù) 消耗日期 ) : 2019-07} ;{mediaYear ( java.lang.String / 媒體數(shù)據(jù)消耗月份 ) : null} ;{vo ( com.agrant.biz.model.vo.ResultVo / view Object ) : ResultVo(code=200, message=success!, data={medias={consumed=[], pending=[{date=2019-06, id=1}, {date=2019-05, id=2}, {date=2019-07, id=3}, {date=2019-02, id=4}, {date=2019-01, id=5}, {date=2019-04, id=6}, {date=2019-03, id=7}]}, agents={consumed=[{date=2019-07-06, id=1}], pending=[{date=2019-07-01, id=2012}, {date=2019-07-01, id=3651}, {date=2019-07-02, id=3661}]}, timestamp=1564486108687}, clazz=null, currentPage=null, pageSize=null, totalPage=null, totalSize=null, orderBy=null)} > return: ResultVo(code=200, message=success!, data={medias={consumed=[], pending=[{date=2019-06, id=1}, {date=2019-05, id=2}]}, agents={consumed=[{date=2019-07-06, id=1}], pending=[{date=2019-07-01, id=2012}, {date=2019-07-01, id=3651}]}, timestamp=1564486108687}, clazz=null, currentPage=null, pageSize=null, totalPage=null, totalSize=null, orderBy=null) } ****用戶:張三 IP:127.0.0.1 操作:請(qǐng)求消耗信息輸入頁(yè)****
?
來(lái)源:https://www.icode9.com/content-1-371051.html聯(lián)系客服