spring boot 自定义Log注解简单版

**

需求是:简单记录用户的操作日志,注意:只看方法不要copy 手打无法执行

**
1.在yml中将spring-aop-auto:true;注意:操作日志类(optertorLog)和opterLogServiceHandler的类都已经创建好 ;本文章已省略;
2,创建Log类

package com.common.annotation;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
    String logType() default "";

3,创建LogAspect 日志切面类

import java.lang.reflect.Method;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.shiro.SecurityUtils;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
 
import com.fasterxml.jackson.databind.ObjectMapper;
i
 
@Aspect
@Component
public class LogAspect {
 
	@Autowired
	private OpterLogServiceHandler opterLogServiceHandler;
 
 	@Pointcut("@annotation(com.common.annotation.Log)")
	public void Logpointcut() {
	}
 
	@Around("Logpointcut()")
	public Object around(ProceedingJoinPoint point) {
		Object result = null;
		long beginTime = System.currentTimeMillis();
		try {
		//获取注解中的value值
			result = point.proceed();
			MethodSignature signature=(MethodSignature )point.getSignature();
			Method method=signature.getMethod();
			Log aoplog=method,getAnnotation(Log.class);
			//获取request
			RequestAttributes requestAttributes=RequestContextHolder.getReqiestAttrinites();
			HttpServletRequest request=requestAttributes.getReqiest();
			//获取用户 自定义set的 
			TicketUser ticket=request.getSession().getAttribute(TicketUser.session_tick_key);
			//保存业务类 optertorLog
			OptertorLog optertorLog=new OptertorLog();
			....optertorLog.set..
			....optertorLog.set..
			...optertorLog.set..
			opterLogServiceHandler.save(optertorLog);

		} catch (Throwable e) {
			e.printStackTrace();
			LogAspect .log.info("日志切面报错!!"+e.getMessage());
			
		}
				return result;
	}
 //看业务中是否需要记录操作人的ip地址 业务逻辑需求
 //获取Ip
 private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值