feat:elk整合登录日志

This commit is contained in:
haoxr 2021-03-04 14:14:01 +08:00
parent fba3c958f9
commit 5d0b60b981
7 changed files with 120 additions and 54 deletions

View File

@ -43,7 +43,7 @@ public class AuthController {
private TokenEndpoint tokenEndpoint;
@ApiOperation("OAuth2认证生成token")
@ApiOperation(value = "OAuth2认证",notes = "login")
@ApiImplicitParams({
@ApiImplicitParam(name = "grant_type", defaultValue = "password", value = "授权模式", required = true),
@ApiImplicitParam(name = "client_id", defaultValue = "client", value = "Oauth2客户端ID", required = true),

View File

@ -5,23 +5,17 @@ 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* @author HXR
@ -39,33 +33,32 @@ public class LogAspect {
@Around("Log()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.nanoTime();
// 时间统计
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();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method.isAnnotationPresent(ApiOperation.class)) {
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
optLog.setDescription(apiOperation.value());
}
long endTime = System.nanoTime();
optLog.setElapsedTime((int) (endTime - startTime));
optLog.setIp(request.getRemoteUser());
optLog.setUrl(request.getRequestURL().toString());
optLog.setMethod(request.getMethod());
Object result = joinPoint.proceed();
optLog.setStartTime(startTime);
optLog.setElapsedTime(elapsedTime);
optLog.setDescription(description );
optLog.setRequestIp(requestIp);
optLog.setRequestUrl(requestUrl);
optLog.setRequestMethod(requestMethod);
optLog.setResult(result);
Map<String,Object> 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);
log.info(Markers.appendEntries(logMap),JSONUtil.parse(optLog).toString());
log.info(JSONUtil.toJsonStr(optLog));
return result;
}
}

View File

@ -0,0 +1,66 @@
package com.youlai.common.web.aspect;
import cn.hutool.extra.servlet.ServletUtil;
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.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
*/
@Aspect
@Component
@AllArgsConstructor
@Slf4j
@ConditionalOnProperty(value = "spring.application.name",havingValue = "youlai-auth")
public class LoginLogAspect {
@Pointcut("execution(public * com.youlai.auth.controller.AuthController.postAccessToken(..))")
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= ServletUtil.getClientIP(request);
String requestUrl=request.getRequestURL().toString();
String requestMethod=request.getMethod();
LoginLog loginLog = new LoginLog();
loginLog.setStartTime(startTime);
loginLog.setElapsedTime(elapsedTime);
loginLog.setDescription(description );
loginLog.setRequestIp(requestIp);
loginLog.setRequestUrl(requestUrl);
loginLog.setRequestMethod(requestMethod);
loginLog.setResult(result);
log.info(JSONUtil.toJsonStr(loginLog));
return result;
}
}

View File

@ -12,17 +12,16 @@ public class LoginLog {
private String description;
private String ip;
private String requestIp;
private String url;
private String requestUrl;
private String method;
private String requestMethod;
private String gmtStart;
private long startTime;
/**
* 消耗时间
*/
private Integer elapsedTime;
private long elapsedTime;
private Object result;
}

View File

@ -2,6 +2,8 @@ package com.youlai.common.web.pojo.domain;
import lombok.Data;
import java.util.Date;
/**
* @Author haoxr
* @Date 2021-03-01 16:45
@ -12,15 +14,15 @@ public class OptLog {
private String description;
private String ip;
private String requestIp;
private String url;
private String requestUrl;
private String method;
private String requestMethod;
private String gmtStart;
private long startTime;
private Integer elapsedTime;
private long elapsedTime;
private Object result;

View File

@ -1,4 +1,5 @@
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.LogAspect,\
com.youlai.common.web.aspect.LoginLogAspect

View File

@ -2,10 +2,10 @@
<!DOCTYPE configuration>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 属性 -->
<!-- 自定义属性 -->
<springProperty scope="context" name="APP_NAME" source="spring.application.name" defaultValue="default"/>
<!--输出到控制台-->
<!-- 输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
@ -13,9 +13,9 @@
</pattern>
</encoder>
</appender>
<!--开启tcp格式的logstash传输通过TCP协议连接Logstash-->
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>www.juxingtech.cn:4560</destination>
<!-- -->
<appender name="LOGIN_LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>g.youlai.store:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
@ -33,7 +33,7 @@
"class": "%logger",
"message": "%message",
"stack_trace": "%exception{20}",
"appname": "${APP_NAME}"
"appname": "login" <!-- 登录日志记录索引名 logstash-%{appname}-yyyy.MM.dd -->
}
</pattern>
</pattern>
@ -41,9 +41,14 @@
</encoder>
<keepAliveDuration>5 minutes</keepAliveDuration>
</appender>
<root level="INFO">
<!-- additivity="false" 不会将日志反馈到root中 -->
<logger name="com.youlai.common.web.aspect.LoginLogAspect" level="INFO" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="LOGIN_LOGSTASH"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="STASH"/>
</root>
<logger name="io.seata" level="ERROR"></logger>
</configuration>