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

发布时间 2023-09-07 16:29:51作者: 无法修改884

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

不过后来还是在项目中把这部分屏蔽了。

 

 1 @Component
 2 public class FileWatcherRunner implements ApplicationRunner {
 3 
 4     private final FileWatcherService fileWatcherService;
 5 
 6     private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 7 
 8     private final Logger LOGGER = LoggerFactory.getLogger(getClass());
 9 
10     @Autowired
11     public FileWatcherRunner(FileWatcherService fileWatcherService) {
12         this.fileWatcherService = fileWatcherService;
13     }
14 
15     @Override
16     public void run(ApplicationArguments args) throws Exception {
17         // 在应用程序启动时启动文件监控服务
18         String filePath = "/opt/yisuo/macaque/srs/std.log"; // 修改为要监控的文件路径
19         LOGGER.info("{} Start the monitoring file service", sdf.format(new Date()));
20         try {
21             fileWatcherService.startFileWatcher(filePath);
22         } catch (IOException | InterruptedException e) {
23             e.printStackTrace();
24         }
25     }
26 }

 

 1 @Service
 2 public class FileWatcherService {
 3 
 4     private final Logger LOGGER = LoggerFactory.getLogger(getClass());
 5 
 6     private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 7 
 8     @Autowired
 9     private SRSApiController srsApiController;
10 
11     public void startFileWatcher(String filePath) throws IOException, InterruptedException {
12         Path path = Paths.get(filePath);
13         Path dir = path.getParent();
14         WatchService watchService = FileSystems.getDefault().newWatchService();
15         dir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
16 
17         while (true) {
18             WatchKey key = watchService.take();
19             for (WatchEvent<?> event : key.pollEvents()) {
20                 WatchEvent.Kind<?> kind = event.kind();
21                 Path fileName = (Path) event.context();
22                 if (kind == StandardWatchEventKinds.ENTRY_MODIFY && fileName.equals(path.getFileName())) {
23                     // 读取新生成的数据
24                     readNewDataFromFile(path);
25                 }
26             }
27             boolean valid = key.reset();
28             if (!valid) {
29                 LOGGER.error("{} 无法继续监听目录,退出监控。", sdf.format(new Date()));
30                 break;
31             }
32         }
33     }
34 
35     private void readNewDataFromFile(Path filePath) {
36         try {
37             BufferedReader reader = new BufferedReader(new FileReader(filePath.toFile()));
38             String line;
39             while ((line = reader.readLine()) != null) {
40                 // 处理每行数据
41                 if (line.contains("error code")) {
42                     srsApiController.onCallback();
43                     LOGGER.error("{} SRS line contains error,restart service", sdf.format(new Date()));
44                 }
45             }
46             reader.close();
47         } catch (IOException e) {
48             e.printStackTrace();
49         }
50     }
51 }