|
@@ -1,14 +1,336 @@
|
|
|
package com.scbfkj.uni.service;
|
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import com.jayway.jsonpath.JsonPath;
|
|
|
+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 org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Component
|
|
|
public class LoggerService {
|
|
|
|
|
|
+ private static String logConnection = """
|
|
|
+ {"jdbcUrl":"jdbc:sqlite:logs/log_","driverClassName":"org.sqlite.JDBC"}""";
|
|
|
+
|
|
|
+ private static Map<String, Integer> flag = new HashMap<>();
|
|
|
+
|
|
|
+
|
|
|
+ // 应用
|
|
|
+ public static void logInterface(String appId, String ip, String requestPath, String sessionId, String requestData, String returnData, String returnCode, String returnMessage, String lifecycleId) {
|
|
|
+ try {
|
|
|
+ String tableName = "interfacelog";
|
|
|
+ String connectionStr = getCurrentThreadConnection(tableName);
|
|
|
+
|
|
|
+ HashMap<String, Object> data = new HashMap<>();
|
|
|
+ data.put("applicationid", appId);
|
|
|
+ data.put("requesttime", LocalDateTime.now());
|
|
|
+ data.put("requestip", ip);
|
|
|
+ data.put("requestpath", requestPath);
|
|
|
+ data.put("sessionid", sessionId);
|
|
|
+ data.put("requestdata", requestData);
|
|
|
+ data.put("returndata", returnData);
|
|
|
+ data.put("returncode", returnCode);
|
|
|
+ data.put("returnmessage", returnMessage);
|
|
|
+ data.put("lifecycleid", lifecycleId);
|
|
|
+ DataBase.exec(connectionStr, tableName, Collections.singletonList(data), "1", null, null);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void logService(String ip, String requestPath, String sessionId, String requestData, String returnData, String returnCode, String returnMessage, String lifecycleId) {
|
|
|
+ try {
|
|
|
+ String tableName = "servicelog";
|
|
|
+ String connectionStr = getCurrentThreadConnection(tableName);
|
|
|
+
|
|
|
+ HashMap<String, Object> data = new HashMap<>();
|
|
|
+ data.put("requesttime", LocalDateTime.now());
|
|
|
+ data.put("requestip", ip);
|
|
|
+ data.put("requestpath", requestPath);
|
|
|
+ data.put("sessionid", sessionId);
|
|
|
+ data.put("requestdata", requestData);
|
|
|
+ data.put("returndata", returnData);
|
|
|
+ data.put("returncode", returnCode);
|
|
|
+ data.put("returnmessage", returnMessage);
|
|
|
+ data.put("lifecycleid", lifecycleId);
|
|
|
+ DataBase.exec(connectionStr, tableName, Collections.singletonList(data), "1", null, null);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void logServiceError(String ip, String requestPath, String sessionId, String requestData, String returnData, String returnCode, String returnMessage, String lifecycleId) {
|
|
|
+ try {
|
|
|
+ String tableName = "serviceerrlog";
|
|
|
+ String connectionStr = getCurrentThreadConnection(tableName);
|
|
|
+
|
|
|
+ HashMap<String, Object> data = new HashMap<>();
|
|
|
+ data.put("requesttime", LocalDateTime.now());
|
|
|
+ data.put("requestip", ip);
|
|
|
+ data.put("requestpath", requestPath);
|
|
|
+ data.put("sessionid", sessionId);
|
|
|
+ data.put("requestdata", requestData);
|
|
|
+ data.put("returndata", returnData);
|
|
|
+ data.put("returncode", returnCode);
|
|
|
+ data.put("returnmessage", returnMessage);
|
|
|
+ data.put("lifecycleid", lifecycleId);
|
|
|
+ DataBase.exec(connectionStr, tableName, Collections.singletonList(data), "1", null, null);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void logSystemError(String ip, String requestPath, String sessionId, String requestData, String returnData, String returnCode, String returnMessage) {
|
|
|
+ try {
|
|
|
+ String tableName = "systemerrlog";
|
|
|
+ String connectionStr = getCurrentThreadConnection(tableName);
|
|
|
+
|
|
|
+ HashMap<String, Object> data = new HashMap<>();
|
|
|
+ data.put("requesttime", LocalDateTime.now());
|
|
|
+ data.put("requestip", ip);
|
|
|
+ data.put("requestpath", requestPath);
|
|
|
+ data.put("sessionid", sessionId);
|
|
|
+ data.put("requestdata", requestData);
|
|
|
+ data.put("returndata", returnData);
|
|
|
+ data.put("returncode", returnCode);
|
|
|
+ data.put("returnmessage", returnMessage);
|
|
|
+ DataBase.exec(connectionStr, tableName, Collections.singletonList(data), "1", null, null);
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void logUser(String userId, String appId, String ip, String requestPath, String sessionId, String requestData, String returnData, String returnCode, String returnMessage, String lifecycleId) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ String tableName = "userlog";
|
|
|
+ String connectionStr = getCurrentThreadConnection(tableName);
|
|
|
+
|
|
|
+ HashMap<String, Object> data = new HashMap<>();
|
|
|
+ data.put("applicationid", appId);
|
|
|
+ data.put("requesttime", LocalDateTime.now());
|
|
|
+ data.put("requestip", ip);
|
|
|
+ data.put("requestpath", requestPath);
|
|
|
+ data.put("sessionid", sessionId);
|
|
|
+ data.put("requestdata", requestData);
|
|
|
+ data.put("returndata", returnData);
|
|
|
+ data.put("returncode", returnCode);
|
|
|
+ data.put("returnmessage", returnMessage);
|
|
|
+ data.put("lifecycleid", lifecycleId);
|
|
|
+ data.put("userid", userId);
|
|
|
+ DataBase.exec(connectionStr, tableName, Collections.singletonList(data), "1", null, null);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getCurrentThreadConnection(String targetName) throws Exception {
|
|
|
+ long id = Thread.currentThread().getId();
|
|
|
+
|
|
|
+ String fileName = "logs" + File.separator + "log_" + id + "_" + targetName;
|
|
|
+ File file = new File(fileName);
|
|
|
+ HashMap<String, Object> connectionMap = new HashMap<>();
|
|
|
+
|
|
|
+ connectionMap.put("jdbcUrl", "jdbc:sqlite:" + file.getPath());
|
|
|
+ connectionMap.put("driverClassName", "org.sqlite.JDBC");
|
|
|
+ String newConnection = DataFormatUtil.toString(connectionMap);
|
|
|
+ if (!file.exists()) {
|
|
|
+ String sql = null;
|
|
|
+ if (targetName.equals("interfacelog")) {
|
|
|
+ sql = """
|
|
|
+ CREATE TABLE interfacelog (
|
|
|
+
|
|
|
+ logid Integer
|
|
|
+ primary key autoincrement,
|
|
|
+ requesttime DATETIME,
|
|
|
+ requestip VARCHAR(255),
|
|
|
+ requestpath VARCHAR(255),
|
|
|
+ requestdata TEXT,
|
|
|
+ sessionid VARCHAR(255),
|
|
|
+ returndata TEXT,
|
|
|
+ returncode INT,
|
|
|
+ returnmessage TEXT,
|
|
|
+ applicationid INT,
|
|
|
+ lifecycleid INT
|
|
|
+ )
|
|
|
+ """;
|
|
|
+ } else if (targetName.equals("servicelog")) {
|
|
|
+ sql = """
|
|
|
+ CREATE TABLE servicelog (
|
|
|
+
|
|
|
+ logid Integer
|
|
|
+ primary key autoincrement,
|
|
|
+ requesttime DATETIME,
|
|
|
+ requestip VARCHAR(255),
|
|
|
+ requestpath VARCHAR(255),
|
|
|
+ requestdata TEXT,
|
|
|
+ sessionid VARCHAR(255),
|
|
|
+ returndata TEXT,
|
|
|
+ returncode INT,
|
|
|
+ returnmessage TEXT,
|
|
|
+ lifecycleid INT
|
|
|
+ )
|
|
|
+ """;
|
|
|
+ } else if (targetName.equals("serviceerrlog")) {
|
|
|
+ sql = """
|
|
|
+ CREATE TABLE serviceerrlog (
|
|
|
+
|
|
|
+ logid Integer
|
|
|
+ primary key autoincrement,
|
|
|
+ requesttime DATETIME,
|
|
|
+ requestip VARCHAR(255),
|
|
|
+ requestpath VARCHAR(255),
|
|
|
+ requestdata TEXT,
|
|
|
+ sessionid VARCHAR(255),
|
|
|
+ returndata TEXT,
|
|
|
+ returncode INT,
|
|
|
+ returnmessage TEXT,
|
|
|
+ lifecycleid INT
|
|
|
+ )
|
|
|
+ """;
|
|
|
+ } else if (targetName.equals("systemerrlog")) {
|
|
|
+ sql = """
|
|
|
+ create table systemerrlog
|
|
|
+ (
|
|
|
+ logid Integer
|
|
|
+ primary key autoincrement,
|
|
|
+ requesttime DATETIME,
|
|
|
+ requestip VARCHAR(255),
|
|
|
+ requestpath VARCHAR(255),
|
|
|
+ requestdata TEXT,
|
|
|
+ sessionid VARCHAR(255),
|
|
|
+ returndata TEXT,
|
|
|
+ returncode INT,
|
|
|
+ returnmessage TEXT,
|
|
|
+ lifecycleid INT
|
|
|
+ );
|
|
|
|
|
|
-// 应用
|
|
|
- public static void log(String connection,String sourceName,String appId,String appName,String ip,String sessionId,String input,String output) {
|
|
|
|
|
|
+ """;
|
|
|
+ } else if (targetName.equals("userlog")) {
|
|
|
+ sql = """
|
|
|
+ CREATE TABLE userdolog (
|
|
|
+
|
|
|
+ logid Integer
|
|
|
+ primary key autoincrement,
|
|
|
+ requesttime DATETIME,
|
|
|
+ requestip VARCHAR(255),
|
|
|
+ requestpath VARCHAR(255),
|
|
|
+ requestdata TEXT,
|
|
|
+ sessionid VARCHAR(255),
|
|
|
+ returndata TEXT,
|
|
|
+ returncode INT,
|
|
|
+ returnmessage TEXT,
|
|
|
+ applicationid INT,
|
|
|
+ userid INT,
|
|
|
+ lifecycleid INT
|
|
|
+ )
|
|
|
+ """;
|
|
|
+ }
|
|
|
+// 创建表结构
|
|
|
+ DataBase.exec(newConnection, sql);
|
|
|
+ }
|
|
|
+ return newConnection;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 读取日志并发送到目标存储介质,最后删除已经发送成功的日志
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Map<String, Object> apply(String fileName) {
|
|
|
+ String newConnection = logConnection.replace("log_", fileName);
|
|
|
+ String tableName = fileName.substring(fileName.lastIndexOf("_") + 1);
|
|
|
+ try {
|
|
|
+ List<Map<String, Object>> result = DataBase.query(newConnection, "select * from %s where 1=?".formatted(tableName), Collections.singletonList(new Object[]{1}));
|
|
|
+ if (result.isEmpty()) {
|
|
|
+ if (!flag.containsKey(fileName)) {
|
|
|
+ flag.put(fileName, 0);
|
|
|
+ }
|
|
|
+ int value = flag.get(fileName) + 1;
|
|
|
+ if (value > 10) {
|
|
|
+ new File("logs" + File.separator + fileName).delete();
|
|
|
+ flag.remove(fileName);
|
|
|
+ return UniReturnUtil.success(null);
|
|
|
+ } else {
|
|
|
+ flag.put(fileName, value);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ List<String> targets = Config.targets;
|
|
|
+
|
|
|
+// 删除成功的日志
|
|
|
+ if (!result.isEmpty()) {
|
|
|
+ for (String target : targets) {
|
|
|
+ Object type = JsonPath.read(target, "$.type");
|
|
|
+ if (Objects.isNull(type)) throw new RuntimeException("日志输出目标类型没有配置");
|
|
|
+ switch (type.toString()) {
|
|
|
+ case "DB" -> {
|
|
|
+ DataBase.exec(target, tableName, result, "1", null, null);
|
|
|
+ }
|
|
|
+ case "KAFKA" -> {
|
|
|
+ Kafka.sendMessage(target, tableName, result);
|
|
|
+ }
|
|
|
+ case "ES" -> {
|
|
|
+ Elasticsearch.send(target, tableName, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DataBase.exec(newConnection, tableName, result.stream().map(it -> (Map<String, Object>) new HashMap<String, Object>() {{
|
|
|
+ put("logid", it.get("logid"));
|
|
|
+ }}).toList(), "3", null, null);
|
|
|
+ }
|
|
|
+ return UniReturnUtil.success(null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void sendLog() throws FileNotFoundException {
|
|
|
+ File logDir = new File("logs");
|
|
|
+ if (logDir.exists()) {
|
|
|
+ if (logDir.isDirectory()) {
|
|
|
+ File[] files = logDir.listFiles();
|
|
|
+
|
|
|
+
|
|
|
+ List<File> logs = new ArrayList<>();
|
|
|
+ if (files != null) {
|
|
|
+ logs = Arrays.stream(files).toList();
|
|
|
+ }
|
|
|
+ logs.parallelStream().forEach(it -> {
|
|
|
+ String fileName = it.getName();
|
|
|
+ apply(fileName);
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new FileNotFoundException("logs 不是文件夹");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new FileNotFoundException("日志目录没有找到");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|