优化代码

This commit is contained in:
smallchill 2018-12-28 15:17:35 +08:00
parent abcb3856f9
commit 226151bee3
9 changed files with 39 additions and 39 deletions

View File

@ -54,12 +54,9 @@ public class RequestLogAspect {
MethodSignature ms = (MethodSignature) point.getSignature(); MethodSignature ms = (MethodSignature) point.getSignature();
Method method = ms.getMethod(); Method method = ms.getMethod();
Object[] args = point.getArgs(); Object[] args = point.getArgs();
// 请求参数处理
final Map<String, Object> paraMap = new HashMap<>(16); final Map<String, Object> paraMap = new HashMap<>(16);
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
// 读取方法参数
MethodParameter methodParam = ClassUtil.getMethodParameter(method, i); MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
// PathVariable 参数跳过
PathVariable pathVariable = methodParam.getParameterAnnotation(PathVariable.class); PathVariable pathVariable = methodParam.getParameterAnnotation(PathVariable.class);
if (pathVariable != null) { if (pathVariable != null) {
continue; continue;
@ -70,7 +67,6 @@ public class RequestLogAspect {
if (requestBody != null && object != null) { if (requestBody != null && object != null) {
paraMap.putAll(BeanUtil.toMap(object)); paraMap.putAll(BeanUtil.toMap(object));
} else { } else {
// 参数名
RequestParam requestParam = methodParam.getParameterAnnotation(RequestParam.class); RequestParam requestParam = methodParam.getParameterAnnotation(RequestParam.class);
String paraName; String paraName;
if (requestParam != null && StringUtil.isNotBlank(requestParam.value())) { if (requestParam != null && StringUtil.isNotBlank(requestParam.value())) {
@ -106,6 +102,7 @@ public class RequestLogAspect {
}); });
needRemoveKeys.forEach(paraMap::remove); needRemoveKeys.forEach(paraMap::remove);
// 打印请求 // 打印请求
log.info("================ Request Start ================");
if (paraMap.isEmpty()) { if (paraMap.isEmpty()) {
log.info("===> {}: {}", requestMethod, requestURI); log.info("===> {}: {}", requestMethod, requestURI);
} else { } else {
@ -127,6 +124,7 @@ public class RequestLogAspect {
} finally { } finally {
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
log.info("<=== {}: {} ({} ms)", request.getMethod(), requestURI, tookMs); log.info("<=== {}: {} ({} ms)", request.getMethod(), requestURI, tookMs);
log.info("================ Request End ================");
} }
} }

View File

@ -19,7 +19,7 @@ package org.springblade.core.log.config;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.core.log.aspect.ApiLogAspect; import org.springblade.core.log.aspect.ApiLogAspect;
import org.springblade.core.log.event.ApiLogListener; 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.event.ErrorLogListener;
import org.springblade.core.log.logger.BladeLogger; import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.launch.props.BladeProperties; import org.springblade.core.launch.props.BladeProperties;
@ -64,8 +64,8 @@ public class BladeLogToolAutoConfiguration {
} }
@Bean @Bean
public BladeLogListener bladeEventListener() { public UsualLogListener bladeEventListener() {
return new BladeLogListener(logService, serverInfo, bladeProperties); return new UsualLogListener(logService, serverInfo, bladeProperties);
} }
} }

View File

@ -25,9 +25,9 @@ import java.util.Map;
* *
* @author smallchill * @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); super(source);
} }

View File

@ -22,7 +22,7 @@ import org.springblade.core.launch.props.BladeProperties;
import org.springblade.core.launch.server.ServerInfo; import org.springblade.core.launch.server.ServerInfo;
import org.springblade.core.log.constant.EventConstant; import org.springblade.core.log.constant.EventConstant;
import org.springblade.core.log.feign.ILogClient; 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.secure.utils.SecureUtil;
import org.springblade.core.tool.utils.UrlUtil; import org.springblade.core.tool.utils.UrlUtil;
import org.springblade.core.tool.utils.WebUtil; import org.springblade.core.tool.utils.WebUtil;
@ -43,7 +43,7 @@ import java.util.Map;
@Slf4j @Slf4j
@Component @Component
@AllArgsConstructor @AllArgsConstructor
public class BladeLogListener { public class UsualLogListener {
private final ILogClient logService; private final ILogClient logService;
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
@ -51,22 +51,22 @@ public class BladeLogListener {
@Async @Async
@Order @Order
@EventListener(BladeLogEvent.class) @EventListener(UsualLogEvent.class)
public void saveBladeLog(BladeLogEvent event) { public void saveUsualLog(UsualLogEvent event) {
Map<String, Object> source = (Map<String, Object>) event.getSource(); 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); HttpServletRequest request = (HttpServletRequest) source.get(EventConstant.EVENT_REQUEST);
logBlade.setRequestUri(UrlUtil.getPath(request.getRequestURI())); logUsual.setRequestUri(UrlUtil.getPath(request.getRequestURI()));
logBlade.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER)); logUsual.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER));
logBlade.setMethod(request.getMethod()); logUsual.setMethod(request.getMethod());
logBlade.setParams(WebUtil.getRequestParamString(request)); logUsual.setParams(WebUtil.getRequestParamString(request));
logBlade.setServerHost(serverInfo.getHostName()); logUsual.setServerHost(serverInfo.getHostName());
logBlade.setServiceId(bladeProperties.getName()); logUsual.setServiceId(bladeProperties.getName());
logBlade.setEnv(bladeProperties.getEnv()); logUsual.setEnv(bladeProperties.getEnv());
logBlade.setServerIp(serverInfo.getIPWithPort()); logUsual.setServerIp(serverInfo.getIPWithPort());
logBlade.setCreateBy(SecureUtil.getUserAccount(request)); logUsual.setCreateBy(SecureUtil.getUserAccount(request));
logBlade.setCreateTime(LocalDateTime.now()); logUsual.setCreateTime(LocalDateTime.now());
logService.saveBladeLog(logBlade); logService.saveUsualLog(logUsual);
} }
} }

View File

@ -17,7 +17,7 @@ package org.springblade.core.log.feign;
import org.springblade.core.launch.constant.AppConstant; import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.model.LogApi; 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.log.model.LogError;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
@ -42,8 +42,8 @@ public interface ILogClient {
* @param log * @param log
* @return * @return
*/ */
@PostMapping(API_PREFIX + "/saveBladeLog") @PostMapping(API_PREFIX + "/saveUsualLog")
R<Boolean> saveBladeLog(@RequestBody LogBlade log); R<Boolean> saveUsualLog(@RequestBody LogUsual log);
/** /**
* 保存操作日志 * 保存操作日志

View File

@ -34,8 +34,8 @@ import java.time.LocalDateTime;
* @since 2018-10-12 * @since 2018-10-12
*/ */
@Data @Data
@TableName("blade_log") @TableName("blade_log_usual")
public class LogBlade implements Serializable { public class LogUsual implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -17,10 +17,10 @@
package org.springblade.core.log.publisher; package org.springblade.core.log.publisher;
import org.springblade.core.log.constant.EventConstant; 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.SpringUtil;
import org.springblade.core.tool.utils.WebUtil; 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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap; import java.util.HashMap;
@ -35,14 +35,14 @@ public class BladeLogPublisher {
public static void publishEvent(String level, String id, String data) { public static void publishEvent(String level, String id, String data) {
HttpServletRequest request = WebUtil.getRequest(); HttpServletRequest request = WebUtil.getRequest();
LogBlade logBlade = new LogBlade(); LogUsual logUsual = new LogUsual();
logBlade.setLogLevel(level); logUsual.setLogLevel(level);
logBlade.setLogId(id); logUsual.setLogId(id);
logBlade.setLogData(data); logUsual.setLogData(data);
Map<String, Object> event = new HashMap<>(16); 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); event.put(EventConstant.EVENT_REQUEST, request);
SpringUtil.publishEvent(new BladeLogEvent(event)); SpringUtil.publishEvent(new UsualLogEvent(event));
} }
} }

View File

@ -31,6 +31,8 @@ public class AuthInfo {
private String accessToken; private String accessToken;
@ApiModelProperty(value = "令牌类型") @ApiModelProperty(value = "令牌类型")
private String tokenType; private String tokenType;
@ApiModelProperty(value = "头像")
private String avatar = "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png";
@ApiModelProperty(value = "角色名") @ApiModelProperty(value = "角色名")
private String authority; private String authority;
@ApiModelProperty(value = "用户名") @ApiModelProperty(value = "用户名")

View File

@ -50,7 +50,7 @@ public class SecureUtil {
public final static Integer AUTH_LENGTH = 7; public final static Integer AUTH_LENGTH = 7;
private static String BASE64_SECURITY = DatatypeConverter.printBase64Binary("SpringBlade".getBytes()); private static String BASE64_SECURITY = DatatypeConverter.printBase64Binary("SpringBlade".getBytes());
/**6 /**
* 获取用户信息 * 获取用户信息
* *
* @return * @return