ludc
2023-11-26 c755f7cbcab36626eadabbe2a77666f9e4ddaec7
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package com.vci.ubcs.log.service.impl;
 
import com.vci.ubcs.log.enumpack.ServiceNameRoleEnum;
import com.vci.ubcs.log.vo.LocalLogVO;
import com.vci.ubcs.log.service.ILogLocalService;
import com.vci.ubcs.log.entity.LocalLog;
import com.vci.ubcs.resource.utils.FileUtil;
import com.vci.ubcs.resource.utils.ZipUtil;
import com.vci.ubcs.starter.exception.VciBaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
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.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.*;
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.*;
import java.util.stream.Collectors;
 
/**
 * 本地系统日志
 * @author ludc
 * @date 2023/10/31 15:39
 */
@Service
@Slf4j
public class LogLocalServiceImpl implements ILogLocalService, EnvironmentAware {
 
    /**
     * 各个服务存放的的父路径
     */
    @Value("${local-log.parent-path:/data1/ubcs/ubcs-server}")
    private String PARENTPATH;
 
    /**
     * 日志文件的具体位置
     */
    @Value("${local-log.log-path:/target/log}")
    private String LOGPATH;
 
    /**
     * 当前操作系统,是否为windows系统
     */
    private Boolean isWindows = true;
 
    /**
     * 根据当前运行的环境,对配置的日志路径格式进行调整
     * @param environment
     */
    @Override
    public void setEnvironment(Environment environment) {
        String os = environment.getProperty("os.name").toLowerCase();
        if (!os.contains("win")) {
            this.PARENTPATH = this.PARENTPATH.substring(this.PARENTPATH.lastIndexOf(":") + 1).replace("\\", "/");
            this.LOGPATH = this.LOGPATH.replace("\\", "/");
            this.isWindows = false;
        }
    }
 
    /**
     * 压缩文件的工具类
     */
    @Resource
    private ZipUtil zipUtil;
 
    /**
     * 获取本地日志列表
     * @param logParentPath
     * @return
     */
    //@Override
    public List<LocalLogVO> getSystemLogList(String logParentPath) {
        List<LocalLogVO> localLogsVO = new ArrayList<>();
        // 不为空说明是加载当前这个服务路径下的日志文件
        if(Func.isNotEmpty(logParentPath)){
            File file = new File(logParentPath);
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                Arrays.stream(files).forEach(item->{
                    // 组建日志文件对象
                    LocalLogVO localLog = new LocalLogVO();
                    localLog.setLogName(item.getName());
                    localLog.setLogType(getLogType(item.getName()));
                    localLog.setCreateTime(getLastModifiedOrCreatTime(false,logParentPath));
                    localLog.setLastModifier(getLastModifiedOrCreatTime(true,logParentPath));
                    localLog.setLogPath(logParentPath);
                    String serviceId = getServiceId(logParentPath);
                    localLog.setServiceId(serviceId);
                    localLog.setServiceName(getServiceName(serviceId));
                    localLog.setHasChildren(false);
                    localLogsVO.add(localLog);
                });
            }
        }else {
            File fileDir = new File(PARENTPATH);
            File[] childDir = fileDir.listFiles();
            Arrays.stream(childDir).forEach(dir->{
                if(dir.getName().contains("ubcs_")){
                    String fullPath = dir.getPath() + LOGPATH;
                    File file = new File(fullPath);
                    if(file.exists()){
                        LocalLogVO localLogVO = new LocalLogVO();
                        localLogVO.setLastModifier(getLastModifiedOrCreatTime(true,fullPath));
                        localLogVO.setCreateTime(getLastModifiedOrCreatTime(false,fullPath));
                        localLogVO.setLogPath(fullPath);
                        String serviceId = getServiceId(file.getPath());
                        localLogVO.setServiceId(serviceId);
                        String serviceName = getServiceName(serviceId);
                        localLogVO.setServiceName(serviceName);
                        localLogVO.setLogType(serviceName+"日志父目录");
                        localLogVO.setLogName(serviceName+"日志父目录");
                        localLogVO.setHasChildren(true);
                        localLogsVO.add(localLogVO);
                    }
                }
            });
        }
        return localLogsVO;
    }
 
    /**
     * 获取文件最后修改或者创建时间
     * @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(this.isWindows ? "\\\\":"/");
        String extractedString = "";
        if(parts.length > 3){
            extractedString = parts[parts.length - 3];
        }
        return extractedString;
    }
 
    /**
     * 获取服务名称
     * @param serViceId
     * @return
     */
    private String getServiceName(String serViceId){
        // EnumCache.getValue(EnumEnum.SERCIVE_NAME_ROLE, serViceId)
        return ServiceNameRoleEnum.getTextByValue(serViceId);
    }
 
    /**
     * 截取路径中的日志文件名称
     * @param logFullPath
     * @return
     */
    private String getLogFileName(String logFullPath){
        // 根据当前操作系统来决定是通过什么字符来截取
        String[] parts = logFullPath.split(this.isWindows ? "\\\\":"/");
        String logFileName = "";
        if(parts.length > 3){
            logFileName = parts[parts.length - 1];
        }
        return logFileName;
    }
 
    /**
     * 下载日志文件
     * @param localLogVO 下载日志对象
     * @return
     * @throws ServerException
     */
    @Override
    public FileObjectBO downloadLogByServiceNameAndFileName(LocalLogVO localLogVO) throws ServerException {
        if(Func.isEmpty(localLogVO) || Func.isEmpty(localLogVO.getLogPath())){
            throw new ServerException("未获取到该日志路径!");
        }
        FileObjectBO fileObjectBO = new FileObjectBO();
        String logFullPaths = localLogVO.getLogFullPaths();
        // 判断是否是父目录
        if(!localLogVO.getHasChildren()){
            //只下载一个日志文件
            File file = new File(logFullPaths);
            if(!file.isFile() || !file.exists()){
                throw new ServerException("本地日志文件路径"+ logFullPaths +"中未找到日志");
            }
            try {
                fileObjectBO.setName(file.getName());
                fileObjectBO.setInputStream(new FileInputStream(file));
                fileObjectBO.setFileLocalPath(logFullPaths);
                fileObjectBO.setFileExtension(".log");
            }catch (Throwable e){
                throw new VciBaseException("获取文件的流有问题",new String[]{logFullPaths},e);
            }
        }else{
            // 是父目录,所以需要获取到下面的所有子目录
            // 有多个,需要使用zip进行压缩
            String tempFolder = FileUtil.getDefaultTempFolder();
            File[] file1 = new File(logFullPaths).listFiles();
            if(file1.length > 0){
                Arrays.stream(file1).forEach(item->{
                    String fileName = tempFolder + File.separator + System.currentTimeMillis() + ".log";
                    File file = new File(fileName);
                    try {
                        if(!file.exists()) {
                            file.createNewFile();
                        }
                    }catch (Throwable e){
                        throw new VciBaseException("创建文件出错,{0}",new String[]{fileName});
                    }
                    File logFile = new File(item.getPath());
                    if(!logFile.exists() || !logFile.isFile()){
                        throw new VciBaseException("本地日志文件路径"+item.getPath()+"中未找到日志");
                    }
 
                    try(OutputStream os = new FileOutputStream(file);
                        InputStream ins = new FileInputStream(logFile);
                    ){
                        IOUtils.copy(ins,os);
                    }catch (Throwable e){
                        throw new VciBaseException("下载文件到临时文件夹里出错,{0}",new String[]{fileName});
                    }
                });
                String zipName = new File(tempFolder).getPath() + File.separator + getLogFileName(logFullPaths) + "等"+file1.length + "个文件.zip";
                zipUtil.folderToZipFile(tempFolder,zipName);
                fileObjectBO.setFileLocalPath(zipName);
                fileObjectBO.setFileExtension(".log");
                if(log.isDebugEnabled()){
                    log.debug("下载文件的信息,",zipName);
                }
            }
        }
        return fileObjectBO;
    }
 
    /**
     * 删除日志文件
     * @param localLogVO 文件全路径集合
     * @throws ServerException
     */
    @Override
    public R deleteLogFile(LocalLogVO localLogVO) throws ServerException {
        List<String> resMsgs = new ArrayList<>();
        // 判断是否是日志顶层目录
        if(localLogVO.getHasChildren()){
            // 是顶层目录,需要循环去删除包含的所有日志文件
            File parentFile = new File(localLogVO.getLogFullPaths());
            if(parentFile.isDirectory()){
                Arrays.stream(parentFile.listFiles()).forEach(logFile->{
                    if (logFile.exists()) {
                        //删除失败的直接记录下文件名
                        if (!logFile.delete()) {
                            resMsgs.add(getLogFileName(logFile.getPath()));
                        }
                    }
                });
            }
        }else {
            // 单个删除
            File file = new File(localLogVO.getLogFullPaths());
            if (file.exists()) {
                //删除失败的直接记录下文件名
                if (!file.delete()) {
                    resMsgs.add(getLogFileName(localLogVO.getLogFullPaths()));
                }
            }
        }
        return resMsgs.size()==0 ? R.success("删除成功!"):R.fail("以下日志文件:"+resMsgs.stream().collect(Collectors.joining(","))+"删除失败!");
    }
 
}