ludc
2023-11-09 6b0971b690de6e50d43b81799ba0378013455cdb
Source/UBCS/ubcs-ops/ubcs-log/src/main/java/com/vci/ubcs/core/log/service/impl/LogSystemServiceImpl.java
@@ -2,16 +2,25 @@
import com.vci.ubcs.core.log.service.ILogSystemService;
import com.vci.ubcs.log.entity.SystemLog;
import com.vci.ubcs.omd.cache.EnumCache;
import com.vci.ubcs.omd.enums.EnumEnum;
import com.vci.ubcs.resource.bo.FileObjectBO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * 本地系统日志
@@ -25,55 +34,136 @@
    * 各个服务存放的的父路径
    */
   private final String parentPath = "/data1/ubcs/ubcs-server";
   /**
    * 各个服务的日志具体的目录路径
    */
   //@Value("#{'${ip-whitelist.ip}'.split(',')}")
   private List<String> serviceDirNames = new ArrayList<>(Arrays.asList("/ubcs_code/target/log","/ubcs_omd/target/log","/ubcs_system/target/log"));
   /**
    * 获取本地日志列表
    * @param logParentPath
    * @return
    */
   @Override
   public List<SystemLog> getSystemLogList() {
      serviceDirNames.stream().forEach(serviceDirName->{
         File file = new File(parentPath+serviceDirName);
         if(file.isDirectory()){
   public List<SystemLog> getSystemLogList(String logParentPath) {
      List<SystemLog> systemLogs = new ArrayList<>();
      // 不为空说明是加载当前这个服务路径下的日志文件
      if(Func.isNotEmpty(logParentPath)){
         File file = new File(logParentPath);
         if (file.isDirectory()) {
            File[] files = file.listFiles();
            for(File f : files){
               if(f.isDirectory()){
                  //readAllFiles(f.getAbsolutePath());
               }else{
                  System.out.println(f.getName());
               }
            }
         }else{
            System.out.println(file.getName());
            Arrays.stream(files).forEach(item->{
               // 组建日志文件对象
               SystemLog systemLog = new SystemLog();
               systemLog.setLogName(item.getName());
               systemLog.setLogType(getLogType(item.getName()));
               systemLog.setCreateTime(getLastModifiedOrCreatTime(false,logParentPath));
               systemLog.setLastmodifier(getLastModifiedOrCreatTime(true,logParentPath));
               systemLog.setLogPath(logParentPath);
               String serviceId = getServiceId(logParentPath);
               systemLog.setServiceId(serviceId);
               systemLog.setServiceName(getServiceName(serviceId));
               systemLogs.add(systemLog);
            });
         }
      });
      return null;
      }else {
         serviceDirNames.stream().forEach(serviceDirName->{
            File file = new File(parentPath+serviceDirName);
            SystemLog systemLog = new SystemLog();
            systemLog.setLastmodifier(getLastModifiedOrCreatTime(true,parentPath+serviceDirName));
            systemLog.setCreateTime(getLastModifiedOrCreatTime(false,parentPath+serviceDirName));
            systemLog.setLogPath(parentPath+serviceDirName);
            String serviceId = getServiceId(logParentPath);
            systemLog.setServiceId(serviceId);
            systemLog.setServiceName(getServiceName(serviceId));
            systemLogs.add(systemLog);
         });
      }
      return systemLogs;
   }
   /**
    * 获取文件最后修改或者创建时间
    * @param isModifier
    * @return
    */
   private String getLastModifiedOrCreatTime(boolean isModifier,String pathStr) {
      Path path = FileSystems.getDefault().getPath(pathStr);
      String date = "";
      try {
         BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         // 是获取最后修改时间
         if(isModifier){
            date = dateFormat.format(new Date(attr.lastModifiedTime().toMillis()));
         }else {
            date = dateFormat.format(new Date(attr.creationTime().toMillis()));
         }
      } catch (IOException e) {
         throw new ServiceException("Error reading file date attributes: " + e.getMessage());
      }
      return date;
   }
   /**
    * 获取日志类型
    * @param fileName
    * @return
    */
   private String getLogType(String fileName){
      //判断日志的的类型
      if (fileName.contains("error")) {
         return "Error";
      } else if (fileName.contains("info")) {
         return "Info";
      } else if (fileName.contains("warning")) {
         return "Warning";
      } else {
         return "Unknown";
      }
   }
   /**
    * 获取服务ID
    * @param servciePath
    * @return
    */
   private String getServiceId(String servciePath){
      String[] parts = servciePath.split("/");
      String extractedString = parts[parts.length - 2];
      return extractedString;
   }
   /**
    * 获取服务名称
    * @param serViceId
    * @return
    */
   private String getServiceName(String serViceId){
      return EnumCache.getValue(EnumEnum.SERCIVE_NAME_ROLE, serViceId);
   }
   /**
    * 下载日志文件
    * @param condition 查询条件map
    * @return
    * @throws ServerException
    */
   @Override
   public FileObjectBO downloadLogByServiceNameAndFileName(Map<String, String> condition) throws ServerException {
      return null;
   }
   /**
    * 删除日志文件
    * @param condition 主键集合
    * @throws ServerException
    */
   @Override
   public void deleteLogFile(Map<String, String> condition) throws ServerException {
   }
}