生产环境是Linux,日志不好查?自己开发一个下载日志功能页面

有时候甲方爸爸的项目要部署内网,日志不能直接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";
}

效果录下

 需要寄快递的朋友,这个小程序发快递只要五块钱哦~

 

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小池先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值