feat:优化日志
This commit is contained in:
parent
b178308817
commit
b9f9cb2612
@ -1,6 +1,6 @@
|
||||
package com.flyfish.framework.domain.po;
|
||||
|
||||
import com.flyfish.framework.domain.base.TreeDomain;
|
||||
import com.flyfish.framework.domain.tree.TreeDomain;
|
||||
import lombok.*;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.flyfish.framework.domain.po;
|
||||
|
||||
import com.flyfish.framework.domain.base.TreeDomain;
|
||||
import com.flyfish.framework.domain.tree.TreeDomain;
|
||||
import com.flyfish.framework.enums.NamedEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.flyfish.framework.domain.tree;
|
||||
|
||||
import org.springframework.data.util.CastUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 根节点表示
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class RootTreeNode<T extends TreeDomain<T>> extends TreeDomain<T> {
|
||||
|
||||
private static final RootTreeNode<?> INSTANCE = new RootTreeNode<>();
|
||||
|
||||
static {
|
||||
INSTANCE.setDepth(0);
|
||||
INSTANCE.setName("根节点");
|
||||
INSTANCE.setParentIds(Collections.emptyList());
|
||||
INSTANCE.setId("0");
|
||||
INSTANCE.setCode("0");
|
||||
}
|
||||
|
||||
public static <T extends TreeDomain<T>> T instance() {
|
||||
return CastUtils.cast(INSTANCE);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.flyfish.framework.domain.base;
|
||||
package com.flyfish.framework.domain.tree;
|
||||
|
||||
import com.flyfish.framework.domain.base.AuditDomain;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.Transient;
|
@ -1,6 +1,8 @@
|
||||
package com.flyfish.framework.domain.base;
|
||||
package com.flyfish.framework.domain.tree;
|
||||
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.domain.base.NameLikeQo;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
@ -45,43 +45,54 @@ public class LogAdvice {
|
||||
LogContext context = LogContext.of(joinPoint);
|
||||
// 结果信息
|
||||
Object result = null;
|
||||
// 是否是publisher
|
||||
boolean reactive = false;
|
||||
try {
|
||||
return result = joinPoint.proceed(joinPoint.getArgs());
|
||||
// 执行方法
|
||||
result = joinPoint.proceed(joinPoint.getArgs());
|
||||
// 判断是否异步
|
||||
reactive = result instanceof Publisher;
|
||||
// 异步需特殊处理,否则直接返回
|
||||
return reactive ? handleResult(result, context) : result;
|
||||
} catch (Throwable throwable) {
|
||||
context.setError(throwable);
|
||||
throw throwable;
|
||||
} finally {
|
||||
if (context.isValid()) {
|
||||
handleResult(result, context);
|
||||
logManager.tryLog(context);
|
||||
if (!reactive && context.isValid()) {
|
||||
context.setResult(result);
|
||||
logManager.tryLog(context.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理结果,支持webflux
|
||||
*
|
||||
* @param result 结果
|
||||
* @param context 上下文
|
||||
*/
|
||||
private void handleResult(Object result, LogContext context) {
|
||||
if (null != result) {
|
||||
// 判断结果是否是publisher
|
||||
if (result instanceof Publisher) {
|
||||
if (result instanceof Mono) {
|
||||
((Mono<?>) result).subscribe(context::setResult, context::setError, context::end);
|
||||
} else if (result instanceof Flux) {
|
||||
((Flux<?>) result).collectList().subscribe(context::setResult, context::setError, context::end);
|
||||
} else {
|
||||
((Publisher<?>) result).subscribe(new LogSubscriber(context));
|
||||
}
|
||||
} else {
|
||||
context.setResult(result);
|
||||
context.end();
|
||||
private Object handleResult(Object result, LogContext context) {
|
||||
Runnable runnable = () -> {
|
||||
if (context.isValid()) {
|
||||
logManager.tryLog(context.end());
|
||||
}
|
||||
};
|
||||
if (result instanceof Mono) {
|
||||
return ((Mono<?>) result)
|
||||
.doOnSuccess(context::setResult)
|
||||
.doOnError(context::setError)
|
||||
.doOnTerminate(runnable);
|
||||
} else if (result instanceof Flux) {
|
||||
return ((Flux<?>) result)
|
||||
.collectList()
|
||||
.doOnSuccess(context::setResult)
|
||||
.doOnError(context::setError)
|
||||
.doOnTerminate(runnable)
|
||||
.flatMapIterable(list -> list);
|
||||
} else {
|
||||
// 为空,直接标记结束
|
||||
context.end();
|
||||
// 未知,不监听
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,8 +123,9 @@ public class LogContext {
|
||||
return null == error;
|
||||
}
|
||||
|
||||
public void end() {
|
||||
public LogContext end() {
|
||||
this.user = UserContext.sharedContext().map(UserContext::currentUser).map(Domain::getName).orElse("未知");
|
||||
this.endTime = System.currentTimeMillis();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class LogManager implements DisposableBean {
|
||||
// 先不拼接
|
||||
log.setUri(null);
|
||||
// 写入日志
|
||||
logService.create(log);
|
||||
logService.create(log).subscribe();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.flyfish.framework.domain;
|
||||
|
||||
import com.flyfish.framework.domain.base.TreeQo;
|
||||
import com.flyfish.framework.domain.tree.TreeQo;
|
||||
import com.flyfish.framework.domain.po.Department;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
@ -2,7 +2,7 @@ package com.flyfish.framework.domain;
|
||||
|
||||
|
||||
import com.flyfish.framework.builder.CriteriaBuilder;
|
||||
import com.flyfish.framework.domain.base.TreeQo;
|
||||
import com.flyfish.framework.domain.tree.TreeQo;
|
||||
import com.flyfish.framework.domain.po.Permission;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.flyfish.framework.controller;
|
||||
|
||||
import com.flyfish.framework.bean.Result;
|
||||
import com.flyfish.framework.domain.base.Qo;
|
||||
import com.flyfish.framework.domain.base.TreeDomain;
|
||||
import com.flyfish.framework.domain.tree.TreeDomain;
|
||||
import com.flyfish.framework.domain.tree.TreeQo;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@ -18,7 +18,7 @@ import java.util.stream.Collectors;
|
||||
* @param <T>
|
||||
* @param <Q>
|
||||
*/
|
||||
public abstract class TreeController<T extends TreeDomain<T>, Q extends Qo<T>> extends BaseController<T, Q> {
|
||||
public abstract class TreeController<T extends TreeDomain<T>, Q extends TreeQo<T>> extends BaseController<T, Q> {
|
||||
|
||||
/**
|
||||
* 获取权限树
|
||||
@ -27,6 +27,7 @@ public abstract class TreeController<T extends TreeDomain<T>, Q extends Qo<T>> e
|
||||
*/
|
||||
@GetMapping("tree")
|
||||
public Result<List<T>> getTree(Q qo) {
|
||||
qo.setRecursive(true);
|
||||
// 第一步,查询全部,并根据条件筛选
|
||||
List<T> filtered = service.getList(qo);
|
||||
// 第二步,根据父id组成map
|
||||
|
@ -1,11 +1,13 @@
|
||||
package com.flyfish.framework.controller.reactive;
|
||||
|
||||
import com.flyfish.framework.bean.Result;
|
||||
import com.flyfish.framework.domain.base.TreeDomain;
|
||||
import com.flyfish.framework.domain.base.TreeQo;
|
||||
import com.flyfish.framework.domain.tree.RootTreeNode;
|
||||
import com.flyfish.framework.domain.tree.TreeDomain;
|
||||
import com.flyfish.framework.domain.tree.TreeQo;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@ -22,6 +24,22 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public abstract class ReactiveTreeController<T extends TreeDomain<T>, Q extends TreeQo<T>> extends ReactiveBaseController<T, Q> {
|
||||
|
||||
/**
|
||||
* 重写树查询,允许查询根节点
|
||||
*
|
||||
* @param id 主键
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("{id}")
|
||||
@Override
|
||||
public Mono<Result<T>> get(@PathVariable String id) {
|
||||
if (TreeDomain.ROOT.equals(id)) {
|
||||
T root = RootTreeNode.instance();
|
||||
return Mono.just(Result.accept(root));
|
||||
}
|
||||
return reactiveService.getById(id).map(Result::accept).defaultIfEmpty(Result.notFound());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取权限树
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user