Skip to content

Commit 206ca97

Browse files
committed
添加ReportAway上报方式
1 parent d51ed24 commit 206ca97

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

api-boot-project/api-boot-plugins/api-boot-plugin-logging/src/main/java/org/minbox/framework/api/boot/plugin/logging/admin/report/LoggingAdminReport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
package org.minbox.framework.api.boot.plugin.logging.admin.report;
1919

2020
import org.minbox.framework.api.boot.common.exception.ApiBootException;
21+
import org.minbox.framework.api.boot.plugin.logging.ApiBootLog;
2122
import org.springframework.beans.factory.InitializingBean;
2223

24+
import java.util.List;
25+
2326
/**
2427
* Batch Report Request Logs To Admin
2528
*
@@ -34,8 +37,17 @@
3437
public interface LoggingAdminReport extends InitializingBean {
3538
/**
3639
* Report Request Logs To Admin
40+
* Loading a specified number of logs from the cache
3741
*
3842
* @throws ApiBootException ApiBoot Exception
3943
*/
4044
void report() throws ApiBootException;
45+
46+
/**
47+
* Report Specified Request Logs
48+
*
49+
* @param logs Request Logs
50+
* @throws ApiBootException ApiBoot Exception
51+
*/
52+
void report(List<ApiBootLog> logs) throws ApiBootException;
4153
}

api-boot-project/api-boot-plugins/api-boot-plugin-logging/src/main/java/org/minbox/framework/api/boot/plugin/logging/admin/report/support/LoggingAdminReportSupport.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,19 @@ public LoggingAdminReportSupport(LoggingAdminDiscovery adminDiscovery, RestTempl
114114
*/
115115
@Override
116116
public void report() throws ApiBootException {
117+
List<ApiBootLog> logs = null;
117118
try {
118-
List<ApiBootLog> logs = loggingCache.getLogs(numberOfRequestLog);
119-
reportToAdmin(logs);
120-
} catch (Exception e) {
119+
// get log from cache
120+
logs = loggingCache.getLogs(numberOfRequestLog);
121+
// execute report
122+
report(logs);
123+
}
124+
// Recycle the request log when an exception is encountered
125+
catch (Exception e) {
121126
logger.error(e.getMessage(), e);
127+
if (!ObjectUtils.isEmpty(logs)) {
128+
logs.stream().forEach(log -> loggingCache.cache(log));
129+
}
122130
}
123131
}
124132

@@ -129,9 +137,11 @@ public void report() throws ApiBootException {
129137
* report request logs ro admin service
130138
* if admin use spring security, set restTemplate header basic auth info
131139
*
132-
* @param logs
140+
* @param logs Request Logs
141+
* @throws ApiBootException ApiBoot Exception
133142
*/
134-
private void reportToAdmin(List<ApiBootLog> logs) {
143+
@Override
144+
public void report(List<ApiBootLog> logs) throws ApiBootException {
135145
if (ObjectUtils.isEmpty(logs)) {
136146
return;
137147
}
@@ -190,6 +200,6 @@ public void destroy() throws Exception {
190200
// get all cache logs
191201
List<ApiBootLog> logs = loggingCache.getAll();
192202
// report to admin
193-
reportToAdmin(logs);
203+
report(logs);
194204
}
195205
}

api-boot-project/api-boot-plugins/api-boot-plugin-logging/src/main/java/org/minbox/framework/api/boot/plugin/logging/notice/away/support/ApiBootLoggingAdminStorageNotice.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
package org.minbox.framework.api.boot.plugin.logging.notice.away.support;
1919

2020
import org.minbox.framework.api.boot.plugin.logging.ApiBootLog;
21+
import org.minbox.framework.api.boot.plugin.logging.ReportAway;
22+
import org.minbox.framework.api.boot.plugin.logging.admin.report.LoggingAdminReport;
2123
import org.minbox.framework.api.boot.plugin.logging.cache.LoggingCache;
2224
import org.minbox.framework.api.boot.plugin.logging.notice.away.ApiBootLogStorageNotice;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
2527

28+
import java.util.Arrays;
29+
2630
/**
2731
* ApiBoot Logging Admin Notice
2832
* report every request log to api-boot-logging-admin
@@ -44,9 +48,20 @@ public class ApiBootLoggingAdminStorageNotice implements ApiBootLogStorageNotice
4448
* Logging Cache
4549
*/
4650
private LoggingCache loggingCache;
51+
/**
52+
* Report Away
53+
*/
54+
private ReportAway reportAway;
55+
/**
56+
* Logging Admin Report
57+
* report request logs to admin
58+
*/
59+
private LoggingAdminReport loggingAdminReport;
4760

48-
public ApiBootLoggingAdminStorageNotice(LoggingCache loggingCache) {
61+
public ApiBootLoggingAdminStorageNotice(LoggingCache loggingCache, ReportAway reportAway, LoggingAdminReport loggingAdminReport) {
4962
this.loggingCache = loggingCache;
63+
this.reportAway = reportAway;
64+
this.loggingAdminReport = loggingAdminReport;
5065
}
5166

5267
/**
@@ -58,5 +73,11 @@ public ApiBootLoggingAdminStorageNotice(LoggingCache loggingCache) {
5873
public void notice(ApiBootLog apiBootLog) {
5974
loggingCache.cache(apiBootLog);
6075
logger.debug("Cache Request Logging Complete.");
76+
// if just report away,execute report logs to admin
77+
switch (reportAway) {
78+
case just:
79+
loggingAdminReport.report(Arrays.asList(apiBootLog));
80+
break;
81+
}
6182
}
6283
}

0 commit comments

Comments
 (0)