xiejun
2023-11-22 47482f386cefd756e1d1a745a5b13949fd709c77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.vci.ubcs.log.service.impl;
 
import com.vci.ubcs.log.service.ILogSystemService;
import com.vci.ubcs.log.entity.SystemLog;
import org.springblade.core.log.exception.ServiceException;
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.tool.utils.Func;
import org.springframework.stereotype.Service;
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.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 本地系统日志
 * @author ludc
 * @date 2023/10/31 15:39
 */
@Service
public class LogSystemServiceImpl implements ILogSystemService{
 
    /**
     * 各个服务存放的的父路径
     */
    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(String logParentPath) {
        List<SystemLog> systemLogs = new ArrayList<>();
        // 不为空说明是加载当前这个服务路径下的日志文件
        if(Func.isNotEmpty(logParentPath)){
            File file = new File(logParentPath);
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                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);
                });
            }
        }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 {
 
    }
 
}