有时候甲方爸爸的项目要部署内网,日志不能直接copy,还是linux系统。排查日志很不方便。
自己搞一个日志下载功能,如果是分布式的项目,还能把其他项目的日志也一起copy下来,来看。
public BiStateDTO<Object> logList(@RequestBody LogParams params) {IPage<LogVO> page = new Page<LogVO>(params.getPageNum(), params.getPageSize());String resourcePath = UploadUtil.getClassResources()+ File.separator+"log";if(StringUtils.isNotEmpty(params.getServer())){// 获取上一级目录的Path对象Path parentDir = Paths.get(UploadUtil.getClassResources()).getParent();resourcePath = parentDir.toAbsolutePath()+ File.separator+params.getServer()+ File.separator+"log";}if(StringUtils.isNotEmpty(params.getSubject()) && params.getServer().equals(LogParams.Server.NETHOSPITAL.getValue())){//互联网项目的admin目录resourcePath = resourcePath +"/" +params.getSubject();if(StringUtils.isEmpty(params.getLogType())){resourcePath = resourcePath +"/info";}}if(StringUtils.isNotEmpty(params.getLogType())){resourcePath = resourcePath +"/" +params.getLogType();}log.info("resourcePath:"+resourcePath);List<LogVO> list = retrieveLogFileNames(resourcePath);if(StringUtils.isNotEmpty(params.getSubject()) && !params.getServer().equals(LogParams.Server.NETHOSPITAL.getValue())){//其他项目日志遍历出带有admin名称的日志list = list.stream().filter(logVO -> logVO.getPath().contains(params.getSubject())).collect(Collectors.toList());}List<LogVO> sortedList = StreamUtils.sortedList(list,LogVO::getLastModify,false);return HandlerUtil.getBiStateDTO(sortedList,sortedList.size()); }
定义一个方法,加载日志列表
下载的时候直接地址请求日志文件路径即可
如果是下载其他同磁盘的目录日志,做个复制文件功能在下载
private Path PathcopyFile(Path resourcePath){Path currentWorkDir = Paths.get(System.getProperty("user.dir"));// 定义目标目录路径Path targetDir = currentWorkDir.resolve("log/temp/");// 如果目标目录不存在,则创建它try {Files.createDirectories(targetDir);} catch (IOException e) {log.error(e.getMessage(),e);return null;}// 定义目标文件路径Path targetPath = targetDir.resolve(resourcePath.getFileName());// 复制文件try {Files.copy(resourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);System.out.println("文件复制成功!");} catch (IOException e) {e.printStackTrace();System.out.println("文件复制失败:" + e.getMessage());}log.info("复制生成:"+targetPath);return targetPath; }
当然 如果你要过滤日志文件的内容,比如通过MDC值或者日志某个用户的信息来过滤
public String getTheadLog(@RequestBody LogParams params) {String sourceFilePath = params.getDownLoadFilePath();String mdcKey=params.getMdcKey();String resourcePath = UploadUtil.getClassResources()+ File.separator+"log";String targetFilePath = resourcePath+"/thread.log"; // 目标文件路径String mdcPrefix = "MDC["+mdcKey+"]"; // MDC前缀Path filePath = Paths.get(targetFilePath);try {// 确保文件的父目录存在//Files.createDirectories(filePath.getParent());// 检查文件是否存在if (!Files.exists(filePath)) {// 文件不存在,则创建它Files.createFile(filePath);} else {Files.deleteIfExists(filePath);Files.createFile(filePath);}} catch (IOException e) {// 处理可能发生的IO异常e.printStackTrace();}if(StringUtils.isNotEmpty(params.getUserName())){//先过滤出用户所有请求的mdc值try {Files.lines(Paths.get(sourceFilePath.toString()), StandardCharsets.UTF_8).filter(line->line.endsWith(FIND_STR+params.getUserName())).forEach(line -> {Matcher mdcMatcher = MDC_PATTERN.matcher(line);if (mdcMatcher.find()) {mdcSet.add(mdcMatcher.group(1));// 提取MDC值}});} catch (IOException e) {log.error(e.getMessage(),e);}try {Files.lines(Paths.get(sourceFilePath.toString()), StandardCharsets.UTF_8).filter(line ->mdcSet.stream().anyMatch(mdc -> line.startsWith(MDC_PREFIX + mdc))).forEach(line -> {try {Files.write(Paths.get(targetFilePath), (line + System.lineSeparator()).getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND, StandardOpenOption.CREATE);} catch (IOException e) {e.printStackTrace();}});} catch (IOException e) {e.printStackTrace();}}else{//根据用户所有的mdc值过滤内容try {Files.lines(Paths.get(sourceFilePath.toString()), StandardCharsets.UTF_8).filter(line -> line.startsWith(mdcPrefix)).forEach(line -> {try {Files.write(Paths.get(targetFilePath), (line + System.lineSeparator()).getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND, StandardOpenOption.CREATE);} catch (IOException e) {e.printStackTrace();}});} catch (IOException e) {e.printStackTrace();}}log.info("targetFilePath:"+targetFilePath);return "log/thread.log"; }
效果录下
想做副业的看过来!!!短剧风口,听说过吗?私信带你