本人设计思路:
第一版有点low,后期可优化成多线程处理,可提升性能
1、前端传两个参数:(startTime,endTime)
2、关于路径我是这样设计的:(downloadLog:最终下载路径,projectPath:每个服务总路径,preparePath:预备目录)
3、每个服务的日志名肯定都会有日期,那么我们可以把每个服务的路径放在数组中,循环处理再把每个路径中的日志名的日期提取出来进行时间筛选,把不满足要求的从数组中移除掉
4、逻辑:分为四步
(1、删除预备目录所有文件)
(2、把每个服务中满足要求的日志包遍历打包到预备目录)
(3、把预备目录中所有文件包打包成一个大包到下载目录)
(4、返回下载目录 路径以及包名)
下面直接贴代码:
实现类:
/**
* download the service log impl class
*/
@Override
public String downloadLogPath(ServiceServrInfoDTO dto) {
if (dto.getStartTime() == null || dto.getEndTime() == null) {
throw new ServiceException(CommonStatus.illegalArgument);
}
log.info("The params startTime {}, endTime {}", dto.getStartTime(),dto.getEndTime());
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
//project public path
String projectPath = systemSettings.getProjectPath();
//prepare path
String preparePath = systemSettings.getPreparePath();
//downloadLog path
String downloadPath = systemSettings.getDownloadLog();
List<String> projectPathList = new ArrayList<>();
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_MANAGER + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_MONITOR + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_SCHEDULER + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_COMPARATOR + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_LIBRARY + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_SNAPSHOT + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_TRANSCODER + "/logs");
projectPathList.add(projectPath + SystemConsts.SERVICE_NAME_FACE_BOOTSTRAP + "/logs");
//the first step, is to delete the prepared directory file
HandlerFileUtils.deleteAllFile(preparePath);
//the second step, is to package each service log to the ready directory
projectPathList.forEach(t ->{
String fileName = t.split("/")[5] + "_" + sDateFormat.format(new Date());
log.info("preparePath path {}, preparePath {}, fileName {}", t,preparePath,fileName);
HandlerFileUtils.fileToZip(t,preparePath,fileName,dto);
});
//the third step, pack all the files in the ready directory into the download directory
String fileName = "logs_" + sDateFormat.format(new Date());
log.info("downloadPath preparePath {}, downloadPath {}, fileName {}", preparePath,downloadPath,fileName);
HandlerFileUtils.fileToZip(preparePath, downloadPath, fileName);
//the four step, return result
String result = downloadPath + fileName + ".zip";
log.info("download the service log return result is {}", result);
return result;
}
自写工具类:
package com.arcvideo.berd.face.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.arcvideo.berd.face.modules.serverinfo.ServiceServrInfoDTO;
import lombok.extern.slf4j.Slf4j;
/**
* @author yzhang
*
*/
@Slf4j
public class HandlerFileUtils {
/**
* Package the file under the source FilePath directory as a zip file
* with a fileName name and store it in the zipFilePath path
*
* @param: @param sourceFilePath
* @param: @param zipFilePath
* @param: @param fileName
* @param: @param dto
* @param: @return
* @return: boolean
* @author: YaXin.Zhang
* @date: 2018年7月5日 下午6:38:41
* @since: JDK 1.8.0_101
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName,ServiceServrInfoDTO dto){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
if(sourceFile.exists() == false){
log.error("directory of files to be compressed:sourceFilePath {} not found.", sourceFilePath);
sourceFile.mkdir(); // create path
}
try {
File file = new File(zipFilePath);
if(!file.exists()) {
file.mkdirs();
}
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
log.error("zipFilePath {} The directory exists under the name:" + fileName +".zip" +"package file.", zipFilePath);
}else{
List<File> list = new ArrayList<>();
File[] sourceFiles = sourceFile.listFiles();
for (int j = 0; j < sourceFiles.length; j++) {
list.add(sourceFiles[j]);
}
List<File> list2 = new ArrayList<>();
//extract the date from the file name
Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getName().contains(".gz")) {
Matcher m = p.matcher(list.get(i).getName());
if(m.find()){
try {
Date date = sDateFormat.parse(m.group());
Date start = sDateFormat.parse(dto.getStartTime());
Date end = sDateFormat.parse(dto.getEndTime());
if(date.getTime() >= start.getTime() && date.getTime() <= end.getTime()) {
list2.add(list.get(i));
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else {
if(list.get(i).getName().contains("log.log")) {
list2.add(list.get(i));
}
}
}
if(null == list2 || list2.size()<1){
log.error("Directory of files to be compressed:sourceFilePath {} There is no file in it, no compression required.", sourceFilePath);
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<list2.size();i++){
//Create the ZIP entity and add it to the package
ZipEntry zipEntry = new ZipEntry(list2.get(i).getName());
zos.putNextEntry(zipEntry);
//Read the file to be compressed and write it into the package
fis = new FileInputStream(list2.get(i));
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//close stream
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return flag;
}
/**
* Package the file under the source FilePath directory as a zip file
* with a fileName name and store it in the zipFilePath path
*
* @param: @param sourceFilePath
* @param: @param zipFilePath
* @param: @param fileName
* @param: @return
* @return: boolean
* @author: YaXin.Zhang
* @date: 2018年7月5日 下午7:04:51
* @since: JDK 1.8.0_101
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
log.error("directory of files to be compressed:sourceFilePath {} not found.", sourceFilePath);
sourceFile.mkdir();
}
try {
File file = new File(zipFilePath);
if(!file.exists()) {
file.mkdirs();
}
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
log.error("zipFilePath {} The directory exists under the name:" + fileName +".zip" +"package file.", zipFilePath);
}else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
log.error("Directory of files to be compressed:sourceFilePath {} There is no file in it, no compression required.", sourceFilePath);
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//Create the ZIP entity and add it to the package
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//Read the file to be compressed and write it into the package
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//close the stream
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return flag;
}
/**
* delete all file
* @param: @param datapath
* @return: void
* @author: YaXin.Zhang
* @date: 2018年7月5日 下午6:35:06
* @since: JDK 1.8.0_101
*/
public static void deleteAllFile(String datapath) {
File file = new File(datapath);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (datapath.endsWith(File.separator)) {
temp = new File(datapath + tempList[i]);
} else {
temp = new File(datapath + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
deleteAllFile(datapath + "/" + tempList[i]);
delFolder(datapath + "/" + tempList[i]);
}
}
return;
}
/**
* delete the folder
* @param: @param folderPath
* @return: void
* @author: YaXin.Zhang
* @date: 2018年7月5日 下午6:35:49
* @since: JDK 1.8.0_101
*/
private static void delFolder(String folderPath) {
try {
deleteAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // delete the null folder
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行日志:
The params startTime 2018-07-01, endTime 2018-07-05
preparePath path /usr/local/arcvideo/iface/face-manager/logs, preparePath /mnt/remote/prepare/, fileName face-manager_2018_07_06_11_03_27
preparePath path /usr/local/arcvideo/iface/face-monitor/logs, preparePath /mnt/remote/prepare/, fileName face-monitor_2018_07_06_11_03_27
preparePath path /usr/local/arcvideo/iface/face-scheduler/logs, preparePath /mnt/remote/prepare/, fileName face-scheduler_2018_07_06_11_03_28
preparePath path /usr/local/arcvideo/iface/face-comparator/logs, preparePath /mnt/remote/prepare/, fileName face-comparator_2018_07_06_11_03_28
preparePath path /usr/local/arcvideo/iface/face-library/logs, preparePath /mnt/remote/prepare/, fileName face-library_2018_07_06_11_03_29
preparePath path /usr/local/arcvideo/iface/face-snapshot/logs, preparePath /mnt/remote/prepare/, fileName face-snapshot_2018_07_06_11_03_30
preparePath path /usr/local/arcvideo/iface/face-transcoder/logs, preparePath /mnt/remote/prepare/, fileName face-transcoder_2018_07_06_11_03_36
preparePath path /usr/local/arcvideo/iface/face-deployer/logs, preparePath /mnt/remote/prepare/, fileName face-deployer_2018_07_06_11_03_36
downloadPath preparePath /mnt/remote/prepare/, downloadPath /mnt/remote/downloadLog/, fileName logs_2018_07_06_11_03_36
download the service log return result is /mnt/remote/downloadLog/logs_2018_07_06_11_03_36.zip
每个服务路径:
每个服务日志情况:
预备打包路径:
下载包路径:
作者:YaXin.Zhang