diff --git a/pom.xml b/pom.xml index 86a911972..276a4f12a 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ 1.4.1 2.9.0 2.0.8 + 6.6 @@ -141,6 +142,11 @@ ${knife4j.version} + + net.logstash.logback + logstash-logback-encoder + ${logstash-logback-encoder.version} + diff --git a/youlai-admin/admin-boot/pom.xml b/youlai-admin/admin-boot/pom.xml index 8ff047f3c..f3e73e623 100644 --- a/youlai-admin/admin-boot/pom.xml +++ b/youlai-admin/admin-boot/pom.xml @@ -64,6 +64,12 @@ org.springframework.boot spring-boot-starter-web + + + com.youlai + common-web + ${youlai.version} + diff --git a/youlai-common/common-web/pom.xml b/youlai-common/common-web/pom.xml index aee754888..b63a2d40a 100644 --- a/youlai-common/common-web/pom.xml +++ b/youlai-common/common-web/pom.xml @@ -22,12 +22,6 @@ ${youlai.version} - - com.youlai - common-redis - ${youlai.version} - - org.springframework.boot spring-boot-starter-web @@ -52,6 +46,11 @@ ${swagger-annotations.version} + + net.logstash.logback + logstash-logback-encoder + + diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java deleted file mode 100644 index a95303dd7..000000000 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/LoginLogAspect.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.youlai.common.web.aspect; - -import cn.hutool.json.JSONUtil; -import com.youlai.common.web.pojo.domain.LoginLog; -import io.swagger.annotations.ApiOperation; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.Signature; -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.data.redis.core.RedisTemplate; -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 java.lang.reflect.Method; - -/** - * @Author HXR - * @CreateTime 2021-03-01 16:47 - */ -@Aspect -@Component -@AllArgsConstructor -@Slf4j -public class LoginLogAspect { - - private RedisTemplate redisTemplate; - - @Pointcut("execution(public * com.youlai..*.controller.*.*(..))") - public void Log() { - } - - @Around("Log()") - public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { - log.info("登录日志记录"); - long startTime = System.nanoTime(); - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletRequest request = attributes.getRequest(); - LoginLog loginLog = new LoginLog(); - Signature signature = joinPoint.getSignature(); - MethodSignature methodSignature = (MethodSignature) signature; - Method method = methodSignature.getMethod(); - if (method.isAnnotationPresent(ApiOperation.class)) { - ApiOperation apiOperation = method.getAnnotation(ApiOperation.class); - loginLog.setDescription(apiOperation.value()); - } - long endTime = System.nanoTime(); - loginLog.setElapsedTime((int) (endTime - startTime)); - loginLog.setIp(request.getRemoteUser()); - loginLog.setUrl(request.getRequestURL().toString()); - loginLog.setMethod(request.getMethod()); - redisTemplate.opsForSet().add("log:login", JSONUtil.toJsonStr(loginLog)); - Object result = joinPoint.proceed(); - return result; - } -} diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/OptLogAspect.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/OptLogAspect.java index e76d66a84..c5bf83c49 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/OptLogAspect.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/aspect/OptLogAspect.java @@ -2,22 +2,27 @@ package com.youlai.common.web.aspect; import cn.hutool.json.JSONUtil; import com.youlai.common.web.pojo.domain.LoginLog; +import com.youlai.common.web.pojo.domain.OptLog; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import net.logstash.logback.marker.Markers; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; 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.data.redis.core.RedisTemplate; +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 java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; /** * @Author HXR @@ -26,36 +31,43 @@ import java.lang.reflect.Method; @Aspect @Component @AllArgsConstructor -@Slf4j public class OptLogAspect { - private RedisTemplate redisTemplate; - - @Pointcut("execution(public * com.youlai..*.controller.*.*(..))") + private static final Logger LOGGER = LoggerFactory.getLogger(OptLogAspect.class); + @Pointcut("execution(public * com.youlai..*.controller.*.*(..)) || execution(public * com.youlai.*.controller.*(..))") public void Log() { } @Around("Log()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { - log.info("操作日志记录"); long startTime = System.nanoTime(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); - LoginLog loginLog = new LoginLog(); + OptLog optLog = new OptLog(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method.isAnnotationPresent(ApiOperation.class)) { ApiOperation apiOperation = method.getAnnotation(ApiOperation.class); - loginLog.setDescription(apiOperation.value()); + optLog.setDescription(apiOperation.value()); } long endTime = System.nanoTime(); - loginLog.setElapsedTime((int) (endTime - startTime)); - loginLog.setIp(request.getRemoteUser()); - loginLog.setUrl(request.getRequestURL().toString()); - loginLog.setMethod(request.getMethod()); - redisTemplate.opsForSet().add("log:opt", JSONUtil.toJsonStr(loginLog)); + optLog.setElapsedTime((int) (endTime - startTime)); + optLog.setIp(request.getRemoteUser()); + optLog.setUrl(request.getRequestURL().toString()); + optLog.setMethod(request.getMethod()); Object result = joinPoint.proceed(); + optLog.setResult(result); + + Map logMap = new HashMap<>(); + logMap.put("url",optLog.getUrl()); + logMap.put("method",optLog.getMethod()); + logMap.put("elapsedTime",optLog.getElapsedTime()); + logMap.put("description",optLog.getDescription()); + logMap.put("result",result); + + LOGGER.info("123"); + LOGGER.info(Markers.appendEntries(logMap),JSONUtil.parse(optLog).toString()); return result; } } diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/LoginLog.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/LoginLog.java index b3df4f99f..2ae10ef9a 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/LoginLog.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/LoginLog.java @@ -20,6 +20,9 @@ public class LoginLog { private String gmtStart; + /** + * 消耗时间 + */ private Integer elapsedTime; } diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/OptLog.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/OptLog.java index c38e09bf2..eaf45023b 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/OptLog.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/pojo/domain/OptLog.java @@ -22,4 +22,6 @@ public class OptLog { private Integer elapsedTime; + private Object result; + } diff --git a/youlai-common/common-web/src/main/resources/logback-spring.xml b/youlai-common/common-web/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..5c496d00e --- /dev/null +++ b/youlai-common/common-web/src/main/resources/logback-spring.xml @@ -0,0 +1,221 @@ + + + + + + + + + + + + + + + + + + + DEBUG + + + + ${FILE_LOG_PATTERN} + UTF-8 + + + + ${LOG_FILE_PATH}/debug/${APP_NAME}-%d{yyyy-MM-dd}-%i.log + + ${LOG_FILE_MAX_SIZE:-10MB} + + ${LOG_FILE_MAX_HISTORY:-30} + + + + + + + + ERROR + ACCEPT + DENY + + + + ${FILE_LOG_PATTERN} + UTF-8 + + + + ${LOG_FILE_PATH}/error/${APP_NAME}-%d{yyyy-MM-dd}-%i.log + + ${LOG_FILE_MAX_SIZE:-10MB} + + ${LOG_FILE_MAX_HISTORY:-30} + + + + + + + DEBUG + + ${LOG_STASH_HOST}:5044 + + + + Asia/Shanghai + + + + + { + "project": "youlai-mall", + "level": "%level", + "service": "${APP_NAME:-}", + "pid": "${PID:-}", + "thread": "%thread", + "class": "%logger", + "message": "%message", + "stack_trace": "%exception{20}" + } + + + + + + + + 5 minutes + + + + + + + + ERROR + ACCEPT + DENY + + ${LOG_STASH_HOST}:5044 + + + + Asia/Shanghai + + + + + { + "project": "youlai-mall", + "level": "%level", + "service": "${APP_NAME:-}", + "pid": "${PID:-}", + "thread": "%thread", + "class": "%logger", + "message": "%message", + "stack_trace": "%exception{20}" + } + + + + + + + + 5 minutes + + + + + + + ${LOG_STASH_HOST}:5044 + + + + Asia/Shanghai + + + + + { + "project": "youlai-mall", + "level": "%level", + "service": "${APP_NAME:-}", + "pid": "${PID:-}", + "thread": "%thread", + "class": "%logger", + "message": "%message", + "stack_trace": "%exception{20}" + } + + + + + + + + 5 minutes + + + + + + + ${LOG_STASH_HOST}:5044 + + + + Asia/Shanghai + + + + + { + "project": "youlai-mall", + "level": "%level", + "service": "${APP_NAME:-}", + "class": "%logger", + "message": "%message" + } + + + + + + + + 5 minutes + + + + + + + + + + + + + + + + + + + + + + + + + + + +