Skip to content

Commit 1ed497d

Browse files
本文主要演示两种低侵入方式进行日志记录
1 parent 6f93ed9 commit 1ed497d

File tree

42 files changed

+1990
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1990
-0
lines changed

.gitignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,63 @@
690690
/springboot-transation-after-commit/target/classes/application-jdbc.yml
691691
/springboot-transation-after-commit/target/test-classes/com/github/lybgeek/SpringbootTransationAfterCommitApplicationTests.class
692692
/springboot-transation-after-commit/springboot-transation-after-commit.iml
693+
/springboot-agent/aoplog-springboot-autoconfiguration/aoplog-springboot-autoconfiguration.iml
694+
/springboot-agent/aoplog-springboot-starter/aoplog-springboot-starter.iml
695+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/config/AopLogAutoConfiguration.class
696+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/properties/AopLogProperties.class
697+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/application.yml
698+
/springboot-agent/springboot-javaagent-test/target/classes/application.yml
699+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/exception/BizException.class
700+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/service/constant/HttpStatus.class
701+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/task/JobRpcExecutor.class
702+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/service/LogService.class
703+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/service/impl/LogServiceImpl.class
704+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/service/client/RpcClientService.class
705+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/service/client/impl/RpcClientServiceImpl.class
706+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/dto/RpcRequest$RpcRequestBuilder.class
707+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/dto/RpcRequest.class
708+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/dto/RpcResponse$RpcResponseBuilder.class
709+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/dto/RpcResponse.class
710+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/service/server/RpcServerService.class
711+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/service/server/impl/RpcServerServiceImpl.class
712+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/entity/ServiceLog$ServiceLogBuilder.class
713+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/entity/ServiceLog.class
714+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/advice/ServiceLogAdvice.class
715+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/config/ServiceLogTaskPoolConfig.class
716+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/META-INF/spring.factories
717+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/META-INF/spring-configuration-metadata.json
718+
/springboot-agent/springboot-agent.iml
719+
/springboot-apollo/springboot-apollo.iml
720+
/springboot-custom-eureka/springboot-custom-eureka.iml
721+
/springboot-dbtemplate/springboot-dbtemplate.iml
722+
/springboot-dbtemplate-autoconfigure/springboot-dbtemplate-autoconfigure.iml
723+
/springboot-dbtemplate-starter/springboot-dbtemplate-starter.iml
724+
/springboot-dbtemplate-test/springboot-dbtemplate-test.iml
725+
/springboot-docker/springboot-docker.iml
726+
/springboot-downgrade/springboot-downgrade.iml
727+
/springboot-dynamic-datasource/springboot-dynamic-datasource.iml
728+
/springboot-easyexcel/springboot-easyexcel.iml
729+
/springboot-elasticsearch/springboot-elasticsearch.iml
730+
/springboot-event-driven/springboot-event-driven.iml
731+
/springboot-exclude-web/springboot-exclude-web.iml
732+
/springboot-generate-sqlscript/springboot-generate-sqlscript.iml
733+
/springboot-httpclient/springboot-httpclient.iml
734+
/springboot-agent/springboot-javaagent-log/springboot-javaagent-log.iml
735+
/springboot-agent/springboot-javaagent-test/springboot-javaagent-test.iml
736+
/springboot-learning.iml
737+
/springboot-mongodb/springboot-mongodb.iml
738+
/springboot-office-preview/springboot-office-preview.iml
739+
/springboot-orm/springboot-orm.iml
740+
/springboot-outside-config/springboot-outside-config.iml
741+
/springboot-prometheus/springboot-prometheus.iml
742+
/springboot-redis/springboot-redis.iml
743+
/springboot-scan-annotation/springboot-scan-annotation.iml
744+
/springboot-slice-upload/springboot-slice-upload.iml
745+
/springboot-split-table/springboot-split-table.iml
746+
/springboot-stream-changeDbInfo/springboot-stream-changeDbInfo.iml
747+
/springboot-stream-saveLog/springboot-stream-saveLog.iml
748+
/springboot-swagger/springboot-swagger.iml
749+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/SpringbootJavaagentTestApplication.class
750+
/springboot-agent/aoplog-springboot-autoconfiguration/target/classes/com/github/lybgeek/logaop/constant/SqlConstant.class
751+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/enity/User$UserBuilder.class
752+
/springboot-agent/springboot-javaagent-test/target/classes/com/github/lybgeek/mock/enity/User.class

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<module>springboot-custom-eureka</module>
4242
<module>springboot-sentinel</module>
4343
<module>springboot-transation-after-commit</module>
44+
<module>springboot-agent</module>
4445
</modules>
4546
<parent>
4647
<groupId>org.springframework.boot</groupId>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-agent</artifactId>
7+
<groupId>com.github.lybgeek</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>aoplog-springboot-autoconfiguration</artifactId>
13+
<properties>
14+
<java.version>1.8</java.version>
15+
<fastjson.version>1.2.73</fastjson.version>
16+
<druid.version>1.1.22</druid.version>
17+
</properties>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-jdbc</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-aop</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>com.alibaba</groupId>
36+
<artifactId>druid-spring-boot-starter</artifactId>
37+
<version>${druid.version}</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-configuration-processor</artifactId>
49+
<optional>true</optional>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-autoconfigure</artifactId>
54+
<optional>true</optional>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
59+
<optional>true</optional>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>com.alibaba</groupId>
69+
<artifactId>fastjson</artifactId>
70+
</dependency>
71+
</dependencies>
72+
73+
74+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.lybgeek.logaop.advice;
2+
3+
4+
import com.alibaba.fastjson.JSON;
5+
import com.github.lybgeek.logaop.entity.ServiceLog;
6+
import com.github.lybgeek.logaop.service.LogService;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.aopalliance.intercept.MethodInterceptor;
9+
import org.aopalliance.intercept.MethodInvocation;
10+
11+
import java.lang.reflect.Method;
12+
13+
@Slf4j
14+
public class ServiceLogAdvice implements MethodInterceptor {
15+
16+
private LogService logService;
17+
18+
public ServiceLogAdvice(LogService logService) {
19+
this.logService = logService;
20+
}
21+
22+
@Override
23+
public Object invoke(MethodInvocation invocation) {
24+
25+
long start = System.currentTimeMillis();
26+
long costTime = 0L;
27+
String status = ServiceLog.SUCEESS;
28+
Object result = null;
29+
String respResult = null;
30+
try {
31+
// 原有函数执行
32+
result = invocation.proceed();
33+
respResult = JSON.toJSONString(result);
34+
} catch (Throwable e){
35+
log.error(e.getMessage(),e);
36+
status = ServiceLog.FAIL;
37+
respResult = e.getMessage();
38+
} finally{
39+
costTime = System.currentTimeMillis() - start;
40+
saveLog(invocation.getArguments(), invocation.getMethod(), costTime, status, respResult);
41+
}
42+
return result;
43+
44+
}
45+
46+
private void saveLog(Object[] args, Method method, long costTime, String status, String respResult) {
47+
ServiceLog serviceLog = ServiceLog.builder()
48+
.serviceName(method.getDeclaringClass().getName())
49+
.costTime(costTime)
50+
.methodName(method.getName())
51+
.status(status)
52+
.reqArgs(JSON.toJSONString(args))
53+
.respResult(respResult).build();
54+
logService.saveLog(serviceLog);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.lybgeek.logaop.config;
2+
3+
4+
import com.github.lybgeek.logaop.advice.ServiceLogAdvice;
5+
import com.github.lybgeek.logaop.properties.AopLogProperties;
6+
import com.github.lybgeek.logaop.service.LogService;
7+
import com.github.lybgeek.logaop.service.impl.LogServiceImpl;
8+
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
11+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
12+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.jdbc.core.JdbcTemplate;
16+
17+
@Configuration
18+
@EnableConfigurationProperties(AopLogProperties.class)
19+
@ConditionalOnProperty(prefix = "servicelog",name = "enabled",havingValue = "true",matchIfMissing = true)
20+
public class AopLogAutoConfiguration {
21+
22+
@Autowired
23+
private JdbcTemplate jdbcTemplate;
24+
25+
@Bean
26+
@ConditionalOnMissingBean
27+
public LogService logService(){
28+
return new LogServiceImpl(jdbcTemplate);
29+
}
30+
31+
@Bean
32+
@ConditionalOnMissingBean
33+
public ServiceLogAdvice serviceLogAdvice(){
34+
return new ServiceLogAdvice(logService());
35+
}
36+
37+
@Bean
38+
@ConditionalOnMissingBean
39+
public AspectJExpressionPointcutAdvisor serviceLogAspectJExpressionPointcutAdvisor(AopLogProperties aopLogProperties) {
40+
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
41+
advisor.setExpression(aopLogProperties.getPointcut());
42+
advisor.setAdvice(serviceLogAdvice());
43+
return advisor;
44+
}
45+
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.lybgeek.logaop.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.scheduling.annotation.EnableAsync;
6+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
7+
8+
import java.util.concurrent.Executor;
9+
import java.util.concurrent.ThreadPoolExecutor;
10+
11+
@EnableAsync
12+
@Configuration
13+
public class ServiceLogTaskPoolConfig {
14+
15+
@Bean
16+
public Executor serviceLogTaskExecutor() {
17+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
18+
executor.setCorePoolSize(10);
19+
executor.setMaxPoolSize(20);
20+
executor.setQueueCapacity(200);
21+
executor.setKeepAliveSeconds(60);
22+
executor.setThreadNamePrefix("svcLog-pool-");
23+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
24+
return executor;
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.lybgeek.logaop.constant;
2+
3+
4+
public class SqlConstant {
5+
6+
public static String CREATE_TABLE_SQL = "CREATE TABLE `service_log` (\n" +
7+
" `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',\n" +
8+
" `service_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '服务名称',\n" +
9+
" `method_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '服务方法名',\n" +
10+
" `req_args` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '服务请求参数',\n" +
11+
" `resp_result` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '服务响应参数',\n" +
12+
" `cost_time` bigint DEFAULT NULL COMMENT '请求响应耗时,单位毫秒',\n" +
13+
" `status` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '响应状态,0、成功;1、失败',\n" +
14+
" `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
15+
" PRIMARY KEY (`id`),\n" +
16+
" KEY `idx_service_method` (`service_name`,`method_name`)\n" +
17+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;";
18+
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.lybgeek.logaop.entity;
2+
3+
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.Date;
10+
11+
@Data
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Builder
15+
public class ServiceLog {
16+
17+
public static final String SUCEESS = "0";
18+
19+
public static final String FAIL = "1";
20+
21+
private Long id;
22+
23+
private String serviceName;
24+
25+
private String methodName;
26+
27+
private String reqArgs;
28+
29+
private String respResult;
30+
31+
private long costTime;
32+
33+
private String status;
34+
35+
private Date createTime;
36+
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.lybgeek.logaop.properties;
2+
3+
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import org.springframework.boot.context.properties.ConfigurationProperties;
8+
9+
@Data
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
@ConfigurationProperties(prefix = "servicelog")
13+
public class AopLogProperties {
14+
15+
/**
16+
* serviceLog enabled;default true
17+
*/
18+
private boolean enabled = true;
19+
20+
/**
21+
* pointcut value
22+
*/
23+
private String pointcut;
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.lybgeek.logaop.service;
2+
3+
4+
import com.github.lybgeek.logaop.entity.ServiceLog;
5+
6+
public interface LogService {
7+
8+
Boolean saveLog(ServiceLog serviceLog);
9+
}

0 commit comments

Comments
 (0)