mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-12-12 20:29:27 +08:00
⚡ 优化代码
This commit is contained in:
parent
abcb3856f9
commit
226151bee3
@ -54,12 +54,9 @@ public class RequestLogAspect {
|
||||
MethodSignature ms = (MethodSignature) point.getSignature();
|
||||
Method method = ms.getMethod();
|
||||
Object[] args = point.getArgs();
|
||||
// 请求参数处理
|
||||
final Map<String, Object> paraMap = new HashMap<>(16);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
// 读取方法参数
|
||||
MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
|
||||
// PathVariable 参数跳过
|
||||
PathVariable pathVariable = methodParam.getParameterAnnotation(PathVariable.class);
|
||||
if (pathVariable != null) {
|
||||
continue;
|
||||
@ -70,7 +67,6 @@ public class RequestLogAspect {
|
||||
if (requestBody != null && object != null) {
|
||||
paraMap.putAll(BeanUtil.toMap(object));
|
||||
} else {
|
||||
// 参数名
|
||||
RequestParam requestParam = methodParam.getParameterAnnotation(RequestParam.class);
|
||||
String paraName;
|
||||
if (requestParam != null && StringUtil.isNotBlank(requestParam.value())) {
|
||||
@ -106,6 +102,7 @@ public class RequestLogAspect {
|
||||
});
|
||||
needRemoveKeys.forEach(paraMap::remove);
|
||||
// 打印请求
|
||||
log.info("================ Request Start ================");
|
||||
if (paraMap.isEmpty()) {
|
||||
log.info("===> {}: {}", requestMethod, requestURI);
|
||||
} else {
|
||||
@ -127,6 +124,7 @@ public class RequestLogAspect {
|
||||
} finally {
|
||||
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
|
||||
log.info("<=== {}: {} ({} ms)", request.getMethod(), requestURI, tookMs);
|
||||
log.info("================ Request End ================");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ package org.springblade.core.log.config;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springblade.core.log.aspect.ApiLogAspect;
|
||||
import org.springblade.core.log.event.ApiLogListener;
|
||||
import org.springblade.core.log.event.BladeLogListener;
|
||||
import org.springblade.core.log.event.UsualLogListener;
|
||||
import org.springblade.core.log.event.ErrorLogListener;
|
||||
import org.springblade.core.log.logger.BladeLogger;
|
||||
import org.springblade.core.launch.props.BladeProperties;
|
||||
@ -64,8 +64,8 @@ public class BladeLogToolAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BladeLogListener bladeEventListener() {
|
||||
return new BladeLogListener(logService, serverInfo, bladeProperties);
|
||||
public UsualLogListener bladeEventListener() {
|
||||
return new UsualLogListener(logService, serverInfo, bladeProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,9 +25,9 @@ import java.util.Map;
|
||||
*
|
||||
* @author smallchill
|
||||
*/
|
||||
public class BladeLogEvent extends ApplicationEvent {
|
||||
public class UsualLogEvent extends ApplicationEvent {
|
||||
|
||||
public BladeLogEvent(Map<String, Object> source) {
|
||||
public UsualLogEvent(Map<String, Object> source) {
|
||||
super(source);
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ import org.springblade.core.launch.props.BladeProperties;
|
||||
import org.springblade.core.launch.server.ServerInfo;
|
||||
import org.springblade.core.log.constant.EventConstant;
|
||||
import org.springblade.core.log.feign.ILogClient;
|
||||
import org.springblade.core.log.model.LogBlade;
|
||||
import org.springblade.core.log.model.LogUsual;
|
||||
import org.springblade.core.secure.utils.SecureUtil;
|
||||
import org.springblade.core.tool.utils.UrlUtil;
|
||||
import org.springblade.core.tool.utils.WebUtil;
|
||||
@ -43,7 +43,7 @@ import java.util.Map;
|
||||
@Slf4j
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class BladeLogListener {
|
||||
public class UsualLogListener {
|
||||
|
||||
private final ILogClient logService;
|
||||
private final ServerInfo serverInfo;
|
||||
@ -51,22 +51,22 @@ public class BladeLogListener {
|
||||
|
||||
@Async
|
||||
@Order
|
||||
@EventListener(BladeLogEvent.class)
|
||||
public void saveBladeLog(BladeLogEvent event) {
|
||||
@EventListener(UsualLogEvent.class)
|
||||
public void saveUsualLog(UsualLogEvent event) {
|
||||
Map<String, Object> source = (Map<String, Object>) event.getSource();
|
||||
LogBlade logBlade = (LogBlade) source.get(EventConstant.EVENT_LOG);
|
||||
LogUsual logUsual = (LogUsual) source.get(EventConstant.EVENT_LOG);
|
||||
HttpServletRequest request = (HttpServletRequest) source.get(EventConstant.EVENT_REQUEST);
|
||||
logBlade.setRequestUri(UrlUtil.getPath(request.getRequestURI()));
|
||||
logBlade.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER));
|
||||
logBlade.setMethod(request.getMethod());
|
||||
logBlade.setParams(WebUtil.getRequestParamString(request));
|
||||
logBlade.setServerHost(serverInfo.getHostName());
|
||||
logBlade.setServiceId(bladeProperties.getName());
|
||||
logBlade.setEnv(bladeProperties.getEnv());
|
||||
logBlade.setServerIp(serverInfo.getIPWithPort());
|
||||
logBlade.setCreateBy(SecureUtil.getUserAccount(request));
|
||||
logBlade.setCreateTime(LocalDateTime.now());
|
||||
logService.saveBladeLog(logBlade);
|
||||
logUsual.setRequestUri(UrlUtil.getPath(request.getRequestURI()));
|
||||
logUsual.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER));
|
||||
logUsual.setMethod(request.getMethod());
|
||||
logUsual.setParams(WebUtil.getRequestParamString(request));
|
||||
logUsual.setServerHost(serverInfo.getHostName());
|
||||
logUsual.setServiceId(bladeProperties.getName());
|
||||
logUsual.setEnv(bladeProperties.getEnv());
|
||||
logUsual.setServerIp(serverInfo.getIPWithPort());
|
||||
logUsual.setCreateBy(SecureUtil.getUserAccount(request));
|
||||
logUsual.setCreateTime(LocalDateTime.now());
|
||||
logService.saveUsualLog(logUsual);
|
||||
}
|
||||
|
||||
}
|
@ -17,7 +17,7 @@ package org.springblade.core.log.feign;
|
||||
|
||||
import org.springblade.core.launch.constant.AppConstant;
|
||||
import org.springblade.core.log.model.LogApi;
|
||||
import org.springblade.core.log.model.LogBlade;
|
||||
import org.springblade.core.log.model.LogUsual;
|
||||
import org.springblade.core.log.model.LogError;
|
||||
import org.springblade.core.tool.api.R;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
@ -42,8 +42,8 @@ public interface ILogClient {
|
||||
* @param log
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(API_PREFIX + "/saveBladeLog")
|
||||
R<Boolean> saveBladeLog(@RequestBody LogBlade log);
|
||||
@PostMapping(API_PREFIX + "/saveUsualLog")
|
||||
R<Boolean> saveUsualLog(@RequestBody LogUsual log);
|
||||
|
||||
/**
|
||||
* 保存操作日志
|
||||
|
@ -34,8 +34,8 @@ import java.time.LocalDateTime;
|
||||
* @since 2018-10-12
|
||||
*/
|
||||
@Data
|
||||
@TableName("blade_log")
|
||||
public class LogBlade implements Serializable {
|
||||
@TableName("blade_log_usual")
|
||||
public class LogUsual implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -17,10 +17,10 @@
|
||||
package org.springblade.core.log.publisher;
|
||||
|
||||
import org.springblade.core.log.constant.EventConstant;
|
||||
import org.springblade.core.log.event.BladeLogEvent;
|
||||
import org.springblade.core.log.event.UsualLogEvent;
|
||||
import org.springblade.core.tool.utils.SpringUtil;
|
||||
import org.springblade.core.tool.utils.WebUtil;
|
||||
import org.springblade.core.log.model.LogBlade;
|
||||
import org.springblade.core.log.model.LogUsual;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
@ -35,14 +35,14 @@ public class BladeLogPublisher {
|
||||
|
||||
public static void publishEvent(String level, String id, String data) {
|
||||
HttpServletRequest request = WebUtil.getRequest();
|
||||
LogBlade logBlade = new LogBlade();
|
||||
logBlade.setLogLevel(level);
|
||||
logBlade.setLogId(id);
|
||||
logBlade.setLogData(data);
|
||||
LogUsual logUsual = new LogUsual();
|
||||
logUsual.setLogLevel(level);
|
||||
logUsual.setLogId(id);
|
||||
logUsual.setLogData(data);
|
||||
Map<String, Object> event = new HashMap<>(16);
|
||||
event.put(EventConstant.EVENT_LOG, logBlade);
|
||||
event.put(EventConstant.EVENT_LOG, logUsual);
|
||||
event.put(EventConstant.EVENT_REQUEST, request);
|
||||
SpringUtil.publishEvent(new BladeLogEvent(event));
|
||||
SpringUtil.publishEvent(new UsualLogEvent(event));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ public class AuthInfo {
|
||||
private String accessToken;
|
||||
@ApiModelProperty(value = "令牌类型")
|
||||
private String tokenType;
|
||||
@ApiModelProperty(value = "头像")
|
||||
private String avatar = "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png";
|
||||
@ApiModelProperty(value = "角色名")
|
||||
private String authority;
|
||||
@ApiModelProperty(value = "用户名")
|
||||
|
@ -50,7 +50,7 @@ public class SecureUtil {
|
||||
public final static Integer AUTH_LENGTH = 7;
|
||||
private static String BASE64_SECURITY = DatatypeConverter.printBase64Binary("SpringBlade".getBytes());
|
||||
|
||||
/**6
|
||||
/**
|
||||
* 获取用户信息
|
||||
*
|
||||
* @return
|
||||
|
Loading…
Reference in New Issue
Block a user