diff --git a/len-sys/src/main/java/com/len/core/annotation/Log.java b/len-sys/src/main/java/com/len/core/annotation/Log.java index e1fb2e29eb2feca92b538a6876345c4523910c35..6679e5b289480f9f4ee91650987fc954a7b9c988 100644 --- a/len-sys/src/main/java/com/len/core/annotation/Log.java +++ b/len-sys/src/main/java/com/len/core/annotation/Log.java @@ -4,6 +4,7 @@ import java.lang.annotation.*; /** * 记录日志 + * @author star */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @@ -15,14 +16,15 @@ public @interface Log { */ String desc(); - ; - /** * 类型 curd */ LOG_TYPE type() default LOG_TYPE.ATHOR; - public enum LOG_TYPE { + enum LOG_TYPE { + /** + * 日志操作类型 + */ ADD, UPDATE, DEL, SELECT, ATHOR } } diff --git a/len-sys/src/main/java/com/len/core/annotation/LogAspect.java b/len-sys/src/main/java/com/len/core/annotation/LogAspect.java index a7fc824346971f7b5616b938af92fad5f7419d32..11c6586bdaef50d026cecdc31a423c707890fc38 100644 --- a/len-sys/src/main/java/com/len/core/annotation/LogAspect.java +++ b/len-sys/src/main/java/com/len/core/annotation/LogAspect.java @@ -2,6 +2,7 @@ package com.len.core.annotation; import java.lang.reflect.Method; import java.util.Date; +import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; @@ -12,7 +13,8 @@ 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.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.ui.Model; import org.springframework.web.context.request.RequestAttributes; @@ -29,41 +31,45 @@ import com.len.util.IpUtil; /** * * 为增删改添加监控 + * + * @author star */ @Aspect @Component public class LogAspect { - @Autowired - private SysLogMapper logMapper; + private static final Logger log = LoggerFactory.getLogger(LogAspect.class); - @Pointcut("@annotation(com.len.core.annotation.Log)") - private void pointcut() { + private final SysLogMapper logMapper; + public LogAspect(SysLogMapper logMapper) { + this.logMapper = logMapper; } + @Pointcut("@annotation(com.len.core.annotation.Log)") + private void pointcut() { /* 织入点无需方法体 */ } + @After("pointcut()") - public void insertLogSuccess(JoinPoint jp) { + public void insertLogSuccess(JoinPoint jp) throws InterruptedException { addLog(jp, getDesc(jp)); } - private void addLog(JoinPoint jp, String text) { + private void addLog(JoinPoint jp, String text) throws InterruptedException { Log.LOG_TYPE type = getType(jp); - SysLog log = new SysLog(); + SysLog sysLog = new SysLog(); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); // 一些系统监控 if (requestAttributes != null) { - HttpServletRequest request = - ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); + HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest(); String ip = IpUtil.getIp(request); - log.setIp(ip); + sysLog.setIp(ip); } - log.setCreateTime(new Date()); - log.setType(type.toString()); - log.setText(text); + sysLog.setCreateTime(new Date()); + sysLog.setType(type.toString()); + sysLog.setText(text); Object[] obj = jp.getArgs(); - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); if (obj != null) { for (int i = 0; i < obj.length; i++) { buffer.append("[参数").append(i + 1).append(":"); @@ -71,36 +77,37 @@ public class LogAspect { if (o instanceof Model) { continue; } - String parameter; + String parameter = null; try { parameter = JSON.toJSONString(o); } catch (Exception e) { - continue; + TimeUnit.MILLISECONDS.sleep(0); } buffer.append(parameter); buffer.append("]"); } } - log.setParam(buffer.toString()); + sysLog.setParam(buffer.toString()); try { CurrentUser currentUser = Principal.getCurrentUse(); if (currentUser != null) { - log.setUserName(currentUser.getUsername()); + sysLog.setUserName(currentUser.getUsername()); } } catch (UnavailableSecurityManagerException e) { + log.error(e.getMessage()); } - logMapper.insert(log); + logMapper.insert(sysLog); } /** * 记录异常 * - * @param joinPoint - * @param e + * @param joinPoint 连接点对象 + * @param e 异常对象 */ @AfterThrowing(value = "pointcut()", throwing = "e") - public void afterException(JoinPoint joinPoint, Exception e) { - System.out.print("-----------afterException:" + e.getMessage()); + public void afterException(JoinPoint joinPoint, Exception e) throws InterruptedException { + log.error("-----------afterException:{}", e.getMessage()); addLog(joinPoint, getDesc(joinPoint) + e.getMessage()); } diff --git a/len-web/src/main/java/com/len/LenApplication.java b/len-web/src/main/java/com/len/LenApplication.java index c21eb2c11a94c572c9c5c34477e4c3cb41e0c3ee..46568d9d123b68a70ec58da632742bd0dc7339e7 100644 --- a/len-web/src/main/java/com/len/LenApplication.java +++ b/len-web/src/main/java/com/len/LenApplication.java @@ -4,21 +4,21 @@ import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; -import org.springframework.context.annotation.ComponentScan; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * 项目启动类 + * + * @author lenosp https://gitee.com/zzdevelop/lenosp */ - @EnableWebMvc @EnableTransactionManagement -@ComponentScan({"com.len", "org.activiti"}) @MapperScan(basePackages = {"com.len.mapper"}) -@SpringBootApplication(exclude = {RedisAutoConfiguration.class, - org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, - org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class}) +@SpringBootApplication(scanBasePackages = {"com.len", "org.activiti"}, + exclude = {RedisAutoConfiguration.class, + org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, + org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class}) public class LenApplication { public static void main(String[] args) { diff --git a/len-web/src/main/resources/log4j.properties b/len-web/src/main/resources/log4j.properties index 23a8c451c8faa209796ef78bf7dbfee42c01c2ef..ca1fd7abde0e4054cc8be01b2b244c5449bd7b7e 100644 --- a/len-web/src/main/resources/log4j.properties +++ b/len-web/src/main/resources/log4j.properties @@ -1,2 +1,4 @@ log4j.rootLogger=debug -log4j.appender.appenderNam=org.apache.log4j.DailyRollingFileAppender \ No newline at end of file +log4j.logger.freemarker.cache=ERROR +log4j.logger.freemarker.beans=ERROR +log4j.appender.appenderNam=org.apache.log4j.DailyRollingFileAppender