优化日志监控
This commit is contained in:
parent
6aaf54e36d
commit
6e30f5a9d8
|
@ -0,0 +1,27 @@
|
||||||
|
package com.len.core.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Inherited;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhuxiaomeng
|
||||||
|
* @date 2017/12/28.
|
||||||
|
* @email 154040976@qq.com
|
||||||
|
*
|
||||||
|
* 记录日志
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
@Documented
|
||||||
|
@Inherited
|
||||||
|
public @interface Log {
|
||||||
|
public enum LOG_TYPE{ADD,UPDATE,DEL,SELECT,ATHOR};
|
||||||
|
/**内容*/
|
||||||
|
String desc();
|
||||||
|
/**类型 curd*/
|
||||||
|
LOG_TYPE type() default LOG_TYPE.ADD;
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package com.len.core.annotation;
|
||||||
|
|
||||||
|
import com.len.base.CurrentUser;
|
||||||
|
import com.len.core.shiro.ShiroUtil;
|
||||||
|
import com.len.entity.SysLog;
|
||||||
|
import com.len.mapper.SysLogMapper;
|
||||||
|
import com.len.util.IpUtil;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Date;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.After;
|
||||||
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
|
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.stereotype.Component;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhuxiaomeng
|
||||||
|
* @date 2017/12/28.
|
||||||
|
* @email 154040976@qq.com
|
||||||
|
*
|
||||||
|
* 为增删改添加监控
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class LogAspect {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SysLogMapper logMapper;
|
||||||
|
|
||||||
|
@Pointcut("@annotation(com.len.core.annotation.Log)")
|
||||||
|
private void pointcut() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@After("pointcut()")
|
||||||
|
public void insertLogSuccess(JoinPoint jp){
|
||||||
|
addLog(jp,getDesc(jp));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addLog(JoinPoint jp,String text){
|
||||||
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||||
|
CurrentUser currentUser= ShiroUtil.getCurrentUse();
|
||||||
|
String ip= IpUtil.getIp(request);
|
||||||
|
Log.LOG_TYPE type=getType(jp);
|
||||||
|
SysLog log=new SysLog();
|
||||||
|
log.setIp(ip);
|
||||||
|
log.setCreateTime(new Date());
|
||||||
|
log.setType(type.toString());
|
||||||
|
log.setText(text);
|
||||||
|
if(currentUser!=null){
|
||||||
|
log.setUserName(currentUser.getUsername());
|
||||||
|
logMapper.insert(log);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录异常
|
||||||
|
* @param joinPoint
|
||||||
|
* @param e
|
||||||
|
*/
|
||||||
|
@AfterThrowing(value="pointcut()",throwing="e")
|
||||||
|
public void afterException(JoinPoint joinPoint,Exception e){
|
||||||
|
System.out.print(e.getMessage());
|
||||||
|
addLog(joinPoint,getDesc(joinPoint)+e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String getDesc(JoinPoint joinPoint){
|
||||||
|
MethodSignature methodName = (MethodSignature)joinPoint.getSignature();
|
||||||
|
Method method = methodName.getMethod();
|
||||||
|
return method.getAnnotation(Log.class).desc();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Log.LOG_TYPE getType(JoinPoint joinPoint){
|
||||||
|
MethodSignature methodName = (MethodSignature)joinPoint.getSignature();
|
||||||
|
Method method = methodName.getMethod();
|
||||||
|
return method.getAnnotation(Log.class).type();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.len.core.annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhuxiaomeng
|
||||||
|
* @date 2017/12/28.
|
||||||
|
* @email 154040976@qq.com
|
||||||
|
*/
|
||||||
|
public enum LogType {
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package com.len.config;
|
package com.len.config;
|
||||||
|
|
||||||
|
import com.len.core.annotation.LogAspect;
|
||||||
import com.len.freemarker.MyFreemarkerConfig;
|
import com.len.freemarker.MyFreemarkerConfig;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -28,4 +29,9 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
|
||||||
.addResourceLocations("classpath:/plugin/");
|
.addResourceLocations("classpath:/plugin/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean(name = "logAspect")
|
||||||
|
public LogAspect getLogAspect(){
|
||||||
|
return new LogAspect();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,5 +111,6 @@
|
||||||
<if test="userName!=null and userName!=''"> and user_name like "%" #{userName} "%"</if>
|
<if test="userName!=null and userName!=''"> and user_name like "%" #{userName} "%"</if>
|
||||||
<if test="type!=null and type!=''"> and type like "%" #{type} "%"</if>
|
<if test="type!=null and type!=''"> and type like "%" #{type} "%"</if>
|
||||||
</where>
|
</where>
|
||||||
|
ORDER BY create_time desc
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
Loading…
Reference in New Issue