¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.ubcs.log.service.impl; |
| | | |
| | | 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.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{ |
| | | |
| | | /** |
| | | * å个æå¡åæ¾ççç¶è·¯å¾ |
| | | */ |
| | | @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; |
| | | |
| | | { |
| | | String os = System.getProperty("os.name").toLowerCase(); |
| | | //é»è®¤å°±é
置为windowsçï¼å¦æä¸æ¯å½åç³»ç»ä¸æ¯windowså°±éè¦å¯¹å
¶è½¬æ¢ä¸ºlinuxçæä»¶è·¯å¾æ ¼å¼ |
| | | 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<LocalLog> getSystemLogList(String logParentPath) { |
| | | List<LocalLog> localLogs = new ArrayList<>(); |
| | | // ä¸ä¸ºç©ºè¯´ææ¯å è½½å½åè¿ä¸ªæå¡è·¯å¾ä¸çæ¥å¿æä»¶ |
| | | if(Func.isNotEmpty(logParentPath)){ |
| | | File file = new File(logParentPath); |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | Arrays.stream(files).forEach(item->{ |
| | | // ç»å»ºæ¥å¿æä»¶å¯¹è±¡ |
| | | LocalLog localLog = new LocalLog(); |
| | | 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); |
| | | localLogs.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); |
| | | LocalLog localLog = new LocalLog(); |
| | | localLog.setLastModifier(getLastModifiedOrCreatTime(true,fullPath)); |
| | | localLog.setCreateTime(getLastModifiedOrCreatTime(false,fullPath)); |
| | | localLog.setLogPath(fullPath); |
| | | String serviceId = getServiceId(file.getPath()); |
| | | localLog.setServiceId(serviceId); |
| | | String serviceName = getServiceName(serviceId); |
| | | localLog.setServiceName(serviceName); |
| | | localLog.setLogType(serviceName+"æ¥å¿ç¶ç®å½"); |
| | | localLog.setLogName(serviceName+"æ¥å¿ç¶ç®å½"); |
| | | localLog.setHasChildren(true); |
| | | localLogs.add(localLog); |
| | | } |
| | | }); |
| | | } |
| | | return localLogs; |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶æåä¿®æ¹æè
å建æ¶é´ |
| | | * @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){ |
| | | return EnumCache.getValue(EnumEnum.SERCIVE_NAME_ROLE, 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(","))+"å é¤å¤±è´¥!"); |
| | | } |
| | | |
| | | } |