随springboot项目启动,实时监控日志文件并进行操作

项目中用到了一个开源音视频服务,但是同事的服务有可能导致开源服务崩溃,所以就写了一个实时监控开源服务输出日志的服务,如果日志中有error信息的话就自动重启那个开源服务。

不过后来还是把这部分屏蔽了,毕竟太简单粗暴,但是先分享一下代码。

这部分是随项目启动的部分。

@Component
public class FileWatcherRunner implements ApplicationRunner {

    private final FileWatcherService fileWatcherService;

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Autowired
    public FileWatcherRunner(FileWatcherService fileWatcherService) {
        this.fileWatcherService = fileWatcherService;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 在应用程序启动时启动文件监控服务
        String filePath = "/opt/yisuo/macaque/srs/std.log"; // 修改为要监控的文件路径
        LOGGER.info("{} Start the monitoring file service", sdf.format(new Date()));
        try {
            fileWatcherService.startFileWatcher(filePath);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

这部分是具体的操作逻辑部分。

@Service
public class FileWatcherService {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private SRSApiController srsApiController;

    public void startFileWatcher(String filePath) throws IOException, InterruptedException {
        Path path = Paths.get(filePath);
        Path dir = path.getParent();
        WatchService watchService = FileSystems.getDefault().newWatchService();
        dir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);

        while (true) {
            WatchKey key = watchService.take();
            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();
                Path fileName = (Path) event.context();
                if (kind == StandardWatchEventKinds.ENTRY_MODIFY && fileName.equals(path.getFileName())) {
                    // 读取新生成的数据
                    readNewDataFromFile(path);
                }
            }
            boolean valid = key.reset();
            if (!valid) {
                LOGGER.error("{} 无法继续监听目录,退出监控。", sdf.format(new Date()));
                break;
            }
        }
    }

    private void readNewDataFromFile(Path filePath) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath.toFile()));
            String line;
            while ((line = reader.readLine()) != null) {
                // 处理每行数据
                if (line.contains("error code")) {
                    srsApiController.onCallback();
                    LOGGER.error("{} SRS line contains error,restart service", sdf.format(new Date()));
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值