feat:优化日志

This commit is contained in:
wangyu 2021-12-08 21:52:02 +08:00
parent b178308817
commit b9f9cb2612
12 changed files with 94 additions and 33 deletions

View File

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

View File

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

View File

@ -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);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -57,7 +57,7 @@ public class LogManager implements DisposableBean {
// 先不拼接
log.setUri(null);
// 写入日志
logService.create(log);
logService.create(log).subscribe();
});
}

View File

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

View File

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

View File

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

View File

@ -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());
}
/**
* 获取权限树
*