Browse Source

日志 反射调用错误消息处理

andy 1 year ago
parent
commit
3e427ba1aa

+ 2 - 2
src/main/java/com/scbfkj/uni/library/UniReturnUtil.java

@@ -49,7 +49,7 @@ public class UniReturnUtil {
         return result;
     }
 
-    public static Map<String, Object> fail(Exception e) {
+    public static Map<String, Object> fail(Throwable e) {
         return fail(getMessage(e));
     }
 
@@ -71,7 +71,7 @@ public class UniReturnUtil {
     public static String getMessage(Throwable e) {
         String detailMessage = Arrays.stream(e.getStackTrace()).limit(8).map(it -> "%s.%s:%s".formatted(it.getClassName(), it.getMethodName(), it.getLineNumber())).collect(Collectors.joining("\n"));
         return """
-                异常消息:%s 错误详情:%s
+                异常消息:%s, 错误详情:%s
                 """.formatted(e.getMessage(), detailMessage);
     }
 }

+ 2 - 1
src/main/java/com/scbfkj/uni/library/script/JavaScriptEngineUtil.java

@@ -83,8 +83,9 @@ public class JavaScriptEngineUtil {
         try {
             javaApply = javaRefPool.borrowObject(key);
             return UniReturnUtil.success(javaApply.invokeApply(args));
+        } catch (InvocationTargetException e) {
+            return UniReturnUtil.fail(e.getTargetException());
         } catch (Exception e) {
-            e.printStackTrace();
             return UniReturnUtil.fail(e);
         } finally {
             if (Objects.nonNull(javaApply)) {

+ 5 - 3
src/main/java/com/scbfkj/uni/service/DataProcessService.java

@@ -252,7 +252,8 @@ public class DataProcessService {
 
                         }
                     } else {
-                        return UniReturnUtil.fail(result.get("message").toString());
+                        message = result.get("message").toString();
+                        throw new RuntimeException(message);
                     }
 
                 } else {
@@ -286,9 +287,10 @@ public class DataProcessService {
                 }
                 HashMap<String, Object> logData = new HashMap<>();
                 logData.put("begintime", startDateTime);
-                logData.put("endtime", LocalDateTime.now());
+                LocalDateTime dateTime = LocalDateTime.now();
+                logData.put("endtime", dateTime);
                 logData.put("serviceid", serviceId);
-                logData.put("resource", DataFormatUtil.toString(resource));
+                logData.put("inputdata", DataFormatUtil.toString(resource));
                 logData.put("prepesource", DataFormatUtil.toString(preResource));
                 logData.put("returnmessage", message);
                 logData.put("lifecycleid", lifecycleid);

+ 35 - 34
src/main/java/com/scbfkj/uni/service/LoggerService.java

@@ -3,6 +3,8 @@ package com.scbfkj.uni.service;
 import com.scbfkj.uni.library.DataFormatUtil;
 import com.scbfkj.uni.library.UniReturnUtil;
 import com.scbfkj.uni.process.DataBase;
+import com.scbfkj.uni.process.Elasticsearch;
+import com.scbfkj.uni.process.Kafka;
 import com.scbfkj.uni.system.Config;
 import com.zaxxer.hikari.pool.HikariPool;
 
@@ -72,20 +74,17 @@ public class LoggerService {
         File file = new File(DIR);
 
 //        过滤出当前不使用的文件
-        List<File> logsFiles = Arrays.stream(Objects.requireNonNull(file.listFiles()))
-                .filter(logFile -> {
-                    String fileName = logFile.getName();
-                    return !fileName.equals(currentFileName) && fileName.endsWith("sqlite");
-                }).toList();
+        List<File> logsFiles = Arrays.stream(Objects.requireNonNull(file.listFiles())).filter(logFile -> {
+            String fileName = logFile.getName();
+            return !fileName.equals(currentFileName) && fileName.endsWith("sqlite");
+        }).toList();
 //        记录出错的日志文件
         List<String> errorFileNames = new ArrayList<>();
-        logsFiles.parallelStream()
-                .map(logFile -> {
+        logsFiles.parallelStream().map(logFile -> {
                     String fileName = logFile.getName();
 //                    转成连接字符串
                     return CONNECTION.formatted(DIR + fileName);
-                })
-                .flatMap(connectionStr -> {
+                }).flatMap(connectionStr -> {
 //                    查询数据
                     try {
                         return DataBase.query(connectionStr, "select logid, target, currentfile, datasourceid, expression, datacontent from logs ").stream();
@@ -103,10 +102,7 @@ public class LoggerService {
 
                 })
 //                分组
-                .collect(Collectors.groupingBy((log) -> log.get("target").toString()))
-                .entrySet()
-                .parallelStream()
-                .forEach(stringListEntry -> {
+                .collect(Collectors.groupingBy((log) -> log.get("target").toString())).entrySet().parallelStream().forEach(stringListEntry -> {
                     String targetName = stringListEntry.getKey();
 //                    需要发送的数据
                     List<Map<String, Object>> value = stringListEntry.getValue();
@@ -128,18 +124,15 @@ public class LoggerService {
                             String connectionStr = config.get("connectset").toString();
                             String expression = value1.get(0).get("expression").toString();
                             List<Object> parameters = new ArrayList<>();
-                            if (connectionStr.contains("jdbc:")) {
-                                type = "DB";
-                            }
 
                             switch (type.toUpperCase()) {
-                                case "ES", "KAFKA" -> {
-                                    parameters.add(DataFormatUtil.toMap(connectionStr));
-                                    parameters.add(expression);
-                                    parameters.add(connectionStr);
-                                    parameters.add(targetName);
-                                    parameters.add(datacontentStream.toList());
-                                    DataProcessService.processByAlgorithm("1", parameters);
+                                case "KAFKA" -> {
+                                    List<Object> datas = datacontentStream.map(Object.class::cast).toList();
+                                    Kafka.sendMessage(connectionStr, targetName, datas);
+                                }
+                                case "ES" -> {
+                                    List<String> datas = datacontentStream.toList();
+                                    Elasticsearch.exec(connectionStr, targetName, "CREATE", datas);
                                 }
                                 case "DB" -> {
                                     parameters.add(connectionStr);
@@ -151,27 +144,39 @@ public class LoggerService {
                                     DataProcessService.processByAlgorithm("3", parameters);
                                 }
                             }
+
                         } catch (Exception e) {
 //                        当前数据是在哪一个数据库文件中
                             value.stream().findFirst().map(d -> d.get("currentfile").toString()).ifPresent(errorFileNames::add);
                             System.out.println(UniReturnUtil.getMessage(e));
                         }
                     });
+                    value.stream().findFirst().map(d -> d.get("currentfile").toString()).ifPresent((it) -> {
+                        if (!errorFileNames.contains(it)) {
+                            String connectionStr = CONNECTION.formatted(it);
+                            try {
+                                DataBase.exec(connectionStr, """
+                                        delete
+                                         from logs
+                                         where 1=1""");
+                            } catch (Exception e) {
+                                throw new RuntimeException(e);
+                            } finally {
+                                removeDataSource(connectionStr);
+                            }
+                        }
+                    });
                 });
         cleanFile(logsFiles, errorFileNames);
     }
 
     private static void cleanFile(List<File> logsFiles, List<String> errorFileNames) {
-        logsFiles.stream().filter(f ->
-                errorFileNames.stream().filter(Objects::nonNull).noneMatch(errorFileName ->
-                        errorFileName.contains(f.getName()))
-        ).forEach(f -> {
+        logsFiles.stream().filter(f -> errorFileNames.stream().filter(Objects::nonNull).noneMatch(errorFileName -> errorFileName.contains(f.getName()))).forEach(f -> {
             String connectionStr = CONNECTION.formatted(DIR + f.getName());
             try {
                 DataBase.exec(connectionStr, "delete  from logs");
                 HikariPool hikariPool = DataBase.dataSourcePools.remove(connectionStr);
-                if (Objects.nonNull(hikariPool))
-                    hikariPool.shutdown();
+                if (Objects.nonNull(hikariPool)) hikariPool.shutdown();
 //                删除文件一直不成功 怀疑是数据库连接导致文件被使用导致一直删除不成功
                 f.delete();
                 fileNames.remove(Long.parseLong(f.getName().substring(0, f.getName().indexOf(".sqlite"))));
@@ -202,11 +207,7 @@ public class LoggerService {
 
     public enum LogType {
 
-        USER("userlog"),
-        INTERFACE("interfacelog"),
-        SYSTEM("systemerrlog"),
-        SERVICE("servicelog"),
-        SERVICE_ERR("serviceerrlog");
+        USER("userlog"), INTERFACE("interfacelog"), SYSTEM("systemerrlog"), SERVICE("servicelog"), SERVICE_ERR("serviceerrlog");
 
         private final String name;
 

+ 1 - 1
src/main/java/com/scbfkj/uni/system/Config.java

@@ -16,7 +16,7 @@ public class Config {
 
     private static String containerCode;
     //    本地日志缓存分片 10分钟
-    private static int splitCount = 600000;
+    private static int splitCount = 6000;
 
     /**
      * 不能创建这个类

+ 1 - 1
src/main/java/com/scbfkj/uni/system/SystemInit.java

@@ -89,7 +89,7 @@ public class SystemInit {
 //        日志任务
         ScheduleUtil.startFrequencyTask(LoggerService::sendMessage, 100);
         HashMap<String, Object> occurrencetime = new HashMap<>();
-        occurrencetime.put("occurrencetime", LocalDateTime.now());
+        occurrencetime.put("logtime", LocalDateTime.now());
         LoggerService.log(LoggerService.LogType.SYSTEM, occurrencetime);
 
 //        心跳检查

+ 7 - 0
src/main/resources/application-test.properties

@@ -0,0 +1,7 @@
+db.center.config=hLcDKcDr4MgqYdb8j0gF0nF806yUy1UdEp1nmztEdo5rNL8IZliDj7/feOp2Fc7j19m9jtiwFp5nPvClI1Ni4kxluI8MQepS8nBK3bEzRzsGSswNHa/Sjyw0GK9/ZnOaiD+lDQyI7+fVbmpdvkLy7QE07bpTIjdI1tcLx8Z9QWs=
+db.security.config=Jnj84d14EmSgKEXyAbSH+bratWGkpV89/VA5Er4yQOt7qlnKtGYJzBVJNNYMBdmSlW0G+nqDHMhJQcmHrwbjjChYuGeDcmKSRmvFQ9u7LwqmgEfazzKKoVawXmJ40dMsec2yaFyNnCM92xn1hzHvle5BL7x3kza2htGm+iOqO7Y=
+#log.target=B7xSbq4imA5zapX8kEO42mU/5sA2TyF/Ba2Y/++F3z9Np7iT4ywDUkbRC4w/Xrxv1kMSR8PQMJ4dfYwc3mYj0SJJivN5A5/6hI+ZSQBabfZZrYwaIIRdM1XIk4wo1SIrSCXKzef8X6YUH70R2tnh+Uq6KNNp08KaZ2ZXM8vX5Ss=
+server.port=9500
+app.container.code=test
+app.security.enable=false
+app.debug=true