feat: 新增日志模块

This commit is contained in:
wangyu 2021-01-12 17:43:18 +08:00
parent a994b7d56b
commit a79c27b228
15 changed files with 323 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package com.flyfish.framework.config;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.experimental.Accessors;
/** /**
* 框架配置 * 框架配置
@ -9,7 +10,10 @@ import lombok.Setter;
*/ */
@Setter @Setter
@Getter @Getter
@Accessors(chain = true)
public class FrameworkConfiguration { public class FrameworkConfiguration {
private boolean resultStyle = false; private boolean resultStyle = false;
private boolean debug = false;
} }

View File

@ -11,7 +11,13 @@ public interface Frameworks {
FrameworkConfiguration config = new FrameworkConfiguration(); FrameworkConfiguration config = new FrameworkConfiguration();
static void resultStyle() { static FrameworkConfiguration resultStyle() {
config.setResultStyle(true); config.setResultStyle(true);
return config;
}
static FrameworkConfiguration debug() {
config.setDebug(true);
return config;
} }
} }

View File

@ -0,0 +1,71 @@
package com.flyfish.framework.annotations;
import java.lang.annotation.*;
/**
* 动作描述一个操作
*
* @author wangyu
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Operation {
/**
* 动作名称可选静态变量池和字符串
*
* @return 结果
*/
String value();
/**
* 模块名描述模块如果为空自动获取bean的rest-bean注解
*
* @return 结果
*/
String module() default "";
/**
* 内置类型
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Operation("CREATE")
@interface Create {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Operation("UPDATE")
@interface Update {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Operation("UPDATE_ALL")
@interface UpdateAll {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Operation("DELETE")
@interface Delete {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Operation("SYNC")
@interface Sync {
}
}

View File

@ -1,8 +1,11 @@
package com.flyfish.framework.domain.po; package com.flyfish.framework.domain.po;
import com.flyfish.framework.domain.base.TreeDomain; import com.flyfish.framework.domain.base.TreeDomain;
import com.flyfish.framework.enums.NamedEnum;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
/** /**
@ -23,13 +26,25 @@ public class Permission extends TreeDomain<Permission> {
ROOT.setDepth(0); ROOT.setDepth(0);
} }
/**
* 权限类型分为路由和动作
*/
private Type type = Type.ROUTE;
/** /**
* 是否是管理员权限 * 是否是管理员权限
*/ */
private boolean admin; private boolean admin;
/** /**
* 是否是叶子节点 * 是否是叶子节点
*/ */
private Boolean leaf; private Boolean leaf;
@Getter
@AllArgsConstructor
public enum Type implements NamedEnum {
ROUTE("路由"), ACTION("动作");
private final String name;
}
} }

27
flyfish-logging/pom.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>flyfish-framework</artifactId>
<groupId>com.flyfish.framework</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>flyfish-logging</artifactId>
<description>日志模块</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.flyfish.framework</groupId>
<artifactId>flyfish-web</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
package com.flyfish.framework.logging.advice;
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.springframework.stereotype.Component;
/**
* 日志切面
*
* @author wangyu
*/
@Aspect
@Slf4j
public class LogAdvice {
/**
* 切入注解
*/
@Pointcut("@annotation( com.flyfish.framework.annotations.Operation)")
public void pointCut() {
}
/**
* 环绕切面获取参数
*
* @param joinPoint 切入点
* @return 结果
*/
@Around("pointCut()")
public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
boolean success = false;
try {
Object result = joinPoint.proceed(args);
success = true;
return result;
} finally {
log.info("success: {}, {}", success,joinPoint.getSignature().getName());
}
}
}

View File

@ -0,0 +1,18 @@
package com.flyfish.framework.logging.annotation;
import com.flyfish.framework.logging.config.LoggingConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 启用日志
*
* @author wangyu
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(LoggingConfig.class)
public @interface EnableLogging {
}

View File

@ -0,0 +1,22 @@
package com.flyfish.framework.logging.config;
import com.flyfish.framework.logging.advice.LogAdvice;
import org.springframework.context.annotation.Bean;
/**
* 日志配置
*
* @author wangyu
*/
public class LoggingConfig {
/**
* 启用日志切面
*
* @return 结果
*/
@Bean
public LogAdvice logAdvice() {
return new LogAdvice();
}
}

View File

@ -0,0 +1,17 @@
package com.flyfish.framework.logging.controller;
import com.flyfish.framework.beans.annotations.RestMapping;
import com.flyfish.framework.controller.BaseController;
import com.flyfish.framework.logging.domain.Log;
import com.flyfish.framework.logging.domain.LogQo;
/**
* 日志controller
*
* @author wangyu
*/
@RestMapping("logs")
public class LogController extends BaseController<Log, LogQo> {
}

View File

@ -0,0 +1,53 @@
package com.flyfish.framework.logging.domain;
import com.flyfish.framework.domain.base.Domain;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
* 日志集合
*/
@Document
@Data
@EqualsAndHashCode(callSuper = true)
public class Log extends Domain {
// 业务
private String business;
// 模块
private String module;
// 方法签名
private String signature;
// 请求地址
private String uri;
// 请求参数
private String body;
// 响应结果
private String response;
// 错误详情
private String error;
// 操作耗时
private Long period;
// 操作时间
private Date createTime;
// 操作人
private String operator;
// 访问ip
private String ip;
// 成功与否
private Boolean success;
}

View File

@ -0,0 +1,10 @@
package com.flyfish.framework.logging.domain;
import com.flyfish.framework.domain.base.NameLikeQo;
/**
* 日志查询实体
* @author wangyu
*/
public class LogQo extends NameLikeQo<Log> {
}

View File

@ -0,0 +1,11 @@
package com.flyfish.framework.logging.repository;
import com.flyfish.framework.logging.domain.Log;
import com.flyfish.framework.repository.DefaultRepository;
/**
* 日志仓库
* @author wangyu
*/
public interface LogRepository extends DefaultRepository<Log> {
}

View File

@ -0,0 +1,15 @@
package com.flyfish.framework.logging.service;
import com.flyfish.framework.logging.domain.Log;
import com.flyfish.framework.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
/**
* 日志服务
* @author wangyu
*/
@Service
public class LogService extends BaseServiceImpl<Log> {
}

View File

@ -1,5 +1,6 @@
package com.flyfish.framework.controller; package com.flyfish.framework.controller;
import com.flyfish.framework.annotations.Operation;
import com.flyfish.framework.bean.Result; import com.flyfish.framework.bean.Result;
import com.flyfish.framework.bean.SyncVo; import com.flyfish.framework.bean.SyncVo;
import com.flyfish.framework.configuration.annotations.CurrentUser; import com.flyfish.framework.configuration.annotations.CurrentUser;
@ -49,6 +50,7 @@ public abstract class BaseController<T extends Domain, Q extends Qo<T>> implemen
} }
@PostMapping("") @PostMapping("")
@Operation.Create
public Result<T> create(@RequestBody @Valid T entity, @CurrentUser User user) { public Result<T> create(@RequestBody @Valid T entity, @CurrentUser User user) {
userContext.setUser(user); userContext.setUser(user);
return Result.accept(service.create(entity)); return Result.accept(service.create(entity));
@ -60,24 +62,28 @@ public abstract class BaseController<T extends Domain, Q extends Qo<T>> implemen
} }
@PutMapping("{id}") @PutMapping("{id}")
@Operation.Update
public Result<T> update(@RequestBody @Valid T entity, @CurrentUser User user) { public Result<T> update(@RequestBody @Valid T entity, @CurrentUser User user) {
userContext.setUser(user); userContext.setUser(user);
return Result.accept(service.updateSelectiveById(entity)); return Result.accept(service.updateSelectiveById(entity));
} }
@PutMapping("") @PutMapping("")
@Operation.UpdateAll
public Result<List<T>> updateList(@RequestBody List<T> list, @CurrentUser User user) { public Result<List<T>> updateList(@RequestBody List<T> list, @CurrentUser User user) {
userContext.setUser(user); userContext.setUser(user);
return Result.accept(service.updateBatch(list)); return Result.accept(service.updateBatch(list));
} }
@PutMapping("/sync") @PutMapping("/sync")
@Operation.Sync
public Result<SyncVo<T>> syncList(@RequestBody List<T> list, @CurrentUser User user) { public Result<SyncVo<T>> syncList(@RequestBody List<T> list, @CurrentUser User user) {
userContext.setUser(user); userContext.setUser(user);
return Result.accept(service.sync(list)); return Result.accept(service.sync(list));
} }
@DeleteMapping("{id}") @DeleteMapping("{id}")
@Operation.Delete
public Result<T> remove(@PathVariable String id) { public Result<T> remove(@PathVariable String id) {
service.deleteById(id); service.deleteById(id);
return Result.ok(); return Result.ok();

View File

@ -41,6 +41,7 @@
<module>flyfish-web</module> <module>flyfish-web</module>
<module>flyfish-user</module> <module>flyfish-user</module>
<module>flyfish-file</module> <module>flyfish-file</module>
<module>flyfish-logging</module>
</modules> </modules>
<repositories> <repositories>