|
|
@@ -0,0 +1,129 @@
|
|
|
+package com.ruoyi.common.aws.cloudwatch;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * CloudWatch Logs测试类
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class CloudWatchLogsTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AwsCloudWatchLogsService cloudWatchLogsService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试CloudWatch Logs基本功能
|
|
|
+ */
|
|
|
+ public void testBasicLogging() {
|
|
|
+ System.out.println("=== 测试CloudWatch Logs基本功能 ===");
|
|
|
+
|
|
|
+ // 测试不同级别的日志
|
|
|
+ cloudWatchLogsService.sendInfoLog("系统启动成功 - 服务正常运行");
|
|
|
+ cloudWatchLogsService.sendWarnLog("内存使用率较高 - 当前使用率: 85%");
|
|
|
+ cloudWatchLogsService.sendErrorLog("数据库连接异常 - 连接池耗尽");
|
|
|
+ cloudWatchLogsService.sendFatalLog("系统崩溃 - 核心服务不可用,需要立即处理");
|
|
|
+
|
|
|
+ System.out.println("基本功能测试完成,请查看CloudWatch控制台日志");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试业务操作日志
|
|
|
+ */
|
|
|
+ public void testBusinessLogging() {
|
|
|
+ System.out.println("=== 测试业务操作日志 ===");
|
|
|
+
|
|
|
+ // 模拟业务操作日志
|
|
|
+ cloudWatchLogsService.sendInfoLog("用户登录成功 - 用户名: admin, IP: 192.168.1.100");
|
|
|
+ cloudWatchLogsService.sendInfoLog("订单创建成功 - 订单号: ORD20241128001, 金额: 299.00");
|
|
|
+ cloudWatchLogsService.sendWarnLog("支付处理延迟 - 订单号: ORD20241128002, 延迟时间: 5秒");
|
|
|
+ cloudWatchLogsService.sendErrorLog("库存扣减失败 - 商品ID: PROD001, 库存不足");
|
|
|
+ cloudWatchLogsService.sendFatalLog("支付网关故障 - 所有支付服务不可用,紧急维护中");
|
|
|
+
|
|
|
+ System.out.println("业务操作日志测试完成,请查看CloudWatch控制台日志");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试批量日志发送
|
|
|
+ */
|
|
|
+ public void testBatchLogging() {
|
|
|
+ System.out.println("=== 测试批量日志发送 ===");
|
|
|
+
|
|
|
+ // 批量发送不同级别的日志
|
|
|
+ for (int i = 1; i <= 10; i++) {
|
|
|
+ String message = String.format("批量测试日志 - 序号: %d, 时间: %s",
|
|
|
+ i, java.time.LocalDateTime.now().toString());
|
|
|
+
|
|
|
+ // 根据序号选择不同的日志级别
|
|
|
+ if (i % 4 == 0) {
|
|
|
+ cloudWatchLogsService.sendFatalLog("致命错误 - " + message);
|
|
|
+ } else if (i % 3 == 0) {
|
|
|
+ cloudWatchLogsService.sendErrorLog("错误 - " + message);
|
|
|
+ } else if (i % 2 == 0) {
|
|
|
+ cloudWatchLogsService.sendWarnLog("警告 - " + message);
|
|
|
+ } else {
|
|
|
+ cloudWatchLogsService.sendInfoLog("信息 - " + message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("批量日志发送测试完成,已发送10条测试日志");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试日志发送结果
|
|
|
+ */
|
|
|
+ public void testLoggingWithResult() {
|
|
|
+ System.out.println("=== 测试日志发送结果 ===");
|
|
|
+
|
|
|
+ // 测试各种级别的日志并获取结果
|
|
|
+ boolean infoResult = cloudWatchLogsService.sendInfoLog("信息级别日志测试");
|
|
|
+ boolean warnResult = cloudWatchLogsService.sendWarnLog("警告级别日志测试");
|
|
|
+ boolean errorResult = cloudWatchLogsService.sendErrorLog("错误级别日志测试");
|
|
|
+ boolean fatalResult = cloudWatchLogsService.sendFatalLog("致命级别日志测试");
|
|
|
+
|
|
|
+ System.out.println("日志发送结果:");
|
|
|
+ System.out.println("- INFO级别: " + (infoResult ? "成功" : "失败"));
|
|
|
+ System.out.println("- WARN级别: " + (warnResult ? "成功" : "失败"));
|
|
|
+ System.out.println("- ERROR级别: " + (errorResult ? "成功" : "失败"));
|
|
|
+ System.out.println("- FATAL级别: " + (fatalResult ? "成功" : "失败"));
|
|
|
+
|
|
|
+ // 测试自定义级别
|
|
|
+ boolean customResult = cloudWatchLogsService.sendLog("自定义DEBUG级别测试", "DEBUG");
|
|
|
+ System.out.println("- 自定义DEBUG级别: " + (customResult ? "成功" : "失败"));
|
|
|
+
|
|
|
+ System.out.println("日志发送结果测试完成");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试FATAL级别日志的特定场景
|
|
|
+ */
|
|
|
+ public void testFatalLogging() {
|
|
|
+ System.out.println("=== 测试FATAL级别日志特定场景 ===");
|
|
|
+
|
|
|
+ // 模拟系统崩溃场景
|
|
|
+ cloudWatchLogsService.sendFatalLog("系统检测到严重错误 - 服务即将停止运行");
|
|
|
+ cloudWatchLogsService.sendFatalLog("数据库连接池完全耗尽 - 无法处理任何请求");
|
|
|
+ cloudWatchLogsService.sendFatalLog("内存溢出错误 - JVM堆内存不足,系统崩溃");
|
|
|
+ cloudWatchLogsService.sendFatalLog("关键配置文件丢失 - 系统无法启动");
|
|
|
+ cloudWatchLogsService.sendFatalLog("外部依赖服务全部不可用 - 系统进入紧急状态");
|
|
|
+
|
|
|
+ System.out.println("FATAL级别日志特定场景测试完成");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 运行所有测试
|
|
|
+ */
|
|
|
+ public void runAllTests() {
|
|
|
+ System.out.println("开始运行所有CloudWatch Logs测试...");
|
|
|
+
|
|
|
+ testBasicLogging();
|
|
|
+ testBusinessLogging();
|
|
|
+ testBatchLogging();
|
|
|
+ testLoggingWithResult();
|
|
|
+ testFatalLogging();
|
|
|
+
|
|
|
+ System.out.println("所有测试完成!请查看CloudWatch控制台中的日志记录。");
|
|
|
+ }
|
|
|
+}
|