提交 a270b36d 编写于 作者: H haoxr

feat:springboot整合elk

上级 5d0b60b9
package com.youlai.common.web.aspect;
import cn.hutool.json.JSONUtil;
import com.youlai.common.web.pojo.domain.OptLog;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @author HXR
* @date 2021-03-01 16:47
*/
@Aspect
@Component
@AllArgsConstructor
@Slf4j
public class LogAspect {
@Pointcut("@annotation(io.swagger.annotations.ApiOperation)")
public void Log() {
}
@Around("Log()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
// 时间统计
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
// 获取方法签名
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
String description = signature.getMethod().getAnnotation(ApiOperation.class).value();
// 获取请求信息
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String requestIp=request.getRemoteUser();
String requestUrl=request.getRequestURL().toString();
String requestMethod=request.getMethod();
OptLog optLog = new OptLog();
optLog.setStartTime(startTime);
optLog.setElapsedTime(elapsedTime);
optLog.setDescription(description );
optLog.setRequestIp(requestIp);
optLog.setRequestUrl(requestUrl);
optLog.setRequestMethod(requestMethod);
optLog.setResult(result);
log.info(JSONUtil.toJsonStr(optLog));
return result;
}
}
package com.youlai.common.web.pojo.domain;
import lombok.Data;
import java.util.Date;
/**
* @Author haoxr
* @Date 2021-03-01 16:45
* @Version 1.0.0
*/
@Data
public class OptLog {
private String description;
private String requestIp;
private String requestUrl;
private String requestMethod;
private long startTime;
private long elapsedTime;
private Object result;
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.youlai.common.web.config.WebMvcConfig,\
com.youlai.common.web.exception.GlobalExceptionHandler,\
com.youlai.common.web.aspect.LogAspect,\
com.youlai.common.web.aspect.LoginLogAspect
......@@ -15,7 +15,7 @@
</appender>
<!-- -->
<appender name="LOGIN_LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>g.youlai.store:4560</destination>
<destination>g.youlai.store:5044</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册