Skip to content

Commit

Permalink
✨ Add more test case and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
lzhpo committed Apr 25, 2024
1 parent 4e9639e commit 0ef56cb
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 28 deletions.
80 changes: 52 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,41 @@ public class OrderRegisterFunction {
}
```

#### 2.3 对象 diff
#### 2.3 异步监听日志事件

日志解析完毕之后会发布一个 `LoggerEvent` 事件,可以自定义 Listener 进行处理。

例如:
```java
@Slf4j
@Component
public class LoggerEventListener {

@Async
@EventListener
public void process(LoggerEvent event) {
log.info("Received LoggerEvent: {}", event);
log.info(event.getMessage());
}
}
```

`LoggerEvent` 字段解释:
- logId: 日志编号。
- message: 日志内容。
- operatorId: 操作人编号。
- businessId: 业务编号。
- category: 日志分类。
- tag: 日志标签。
- additional: 日志额外信息。
- createTime: 日志创建时间。
- takeTime: 业务方法耗时,单位:毫秒。
- result: 业务方法执行结果。
- success: 业务方法是否执行成功。
- errors: 业务方法执行期间发生的异常。
- diffResults: 对象diff的结果。

#### 2.4 对象 diff

对象 diff 的意思就是给两个对象,找出它们的区别。

Expand Down Expand Up @@ -149,39 +183,29 @@ logger:
template: "[{filedName}] has been updated from [{oldValue}] to [{newValue}]"
```
#### 2.4 异步监听日志事件
日志解析完毕之后会发布一个 `LoggerEvent` 事件,可以自定义 Listener 进行处理。
同时 diff 也支持排除指定对象或字段。
例如:
```java
@Slf4j
@Component
public class LoggerEventListener {
// 排除此对象diff
@LoggerDiffObject(disabled = true)
public class UserWithDisabledObject {

@Async
@EventListener
public void process(LoggerEvent event) {
log.info("Received LoggerEvent: {}", event);
log.info(event.getMessage());
}
private String username;

private String email;
}
```

`LoggerEvent` 字段解释:
- logId: 日志编号。
- message: 日志内容。
- operatorId: 操作人编号。
- businessId: 业务编号。
- category: 日志分类。
- tag: 日志标签。
- additional: 日志额外信息。
- createTime: 日志创建时间。
- takeTime: 业务方法耗时,单位:毫秒。
- result: 业务方法执行结果。
- success: 业务方法是否执行成功。
- errors: 业务方法执行期间发生的异常。
- diffResults: 对象diff的结果。
```java
public class UserWithDisabledField {

// 排除此字段diff
@LoggerDiffField(disabled = true)
private String username;

private String email;
}
```

#### 2.5 关于`@Logger`注解在IDEA设置SpringEL的提示

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/lzhpo/logger/LoggerAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.lzhpo.logger.domain.Admin;
import com.lzhpo.logger.domain.User;
import com.lzhpo.logger.domain.UserWithDisabledField;
import com.lzhpo.logger.domain.UserWithDisabledObject;
import org.springframework.stereotype.Component;

/**
Expand Down Expand Up @@ -75,6 +76,11 @@ public void userWithDisabledFieldDiff(UserWithDisabledField oldUser, UserWithDis
// NOP
}

@Logger(message = "#DIFF(#oldUser, #newUser)")
public void userWithDisabledObjectDiff(UserWithDisabledObject oldUser, UserWithDisabledObject newUser) {
// NOP
}

@LoggerComponent
public static class LoggerFunctions {

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/lzhpo/logger/LoggerActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.lzhpo.logger.domain.Admin;
import com.lzhpo.logger.domain.User;
import com.lzhpo.logger.domain.UserWithDisabledField;
import com.lzhpo.logger.domain.UserWithDisabledObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -159,4 +160,30 @@ void userWithDisabledFieldDiff() {
assertTrue(message.contains("[email] has been updated from [] to [jack@gmail.com]"));
assertTrue(message.contains("[phone] has been updated from [123456] to []"));
}

@Test
void userWithDisabledObjectDiff() {
UserWithDisabledObject oldUser = UserWithDisabledObject.builder()
.username("Jack")
.age(22)
.email("rose@gmail.com")
.phone("123456")
.build();

UserWithDisabledObject newUser = UserWithDisabledObject.builder()
.username("Rose")
.age(23)
.email("rose@gmail.com")
.phone("456789")
.build();

loggerAction.userWithDisabledObjectDiff(oldUser, newUser);

String message = LoggerTestSupport.getMessage();
assertFalse(StringUtils.hasText(message));
assertFalse(message.contains("[username] has been updated from [Jack] to [Rose]"));
assertFalse(message.contains("[age] has been updated from [22] to [23]"));
assertFalse(message.contains("[email] has been updated from [rose@gmail.com] to [jack@gmail.com]"));
assertFalse(message.contains("[phone] has been updated from [123456] to [456789]"));
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/lzhpo/logger/domain/UserWithDisabledObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright lzhpo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lzhpo.logger.domain;

import com.lzhpo.logger.annotation.LoggerDiffObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author lzhpo
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@LoggerDiffObject(disabled = true)
public class UserWithDisabledObject {

private String username;

private Integer age;

private String email;

private String phone;
}

0 comments on commit 0ef56cb

Please sign in to comment.