feat:springboot整合elk

This commit is contained in:
haoxr 2021-03-04 20:23:15 +08:00
parent 5d0b60b981
commit a270b36d10
4 changed files with 1 additions and 95 deletions

View File

@ -1,64 +0,0 @@
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;
}
}

View File

@ -1,29 +0,0 @@
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;
}

View File

@ -1,5 +1,4 @@
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

View File

@ -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>