Skip to content

Commit

Permalink
fix: Log Collection and Monitoring bug (#825)
Browse files Browse the repository at this point in the history
* feat: add init es sniffer

* feat: update

* fix: log monitoring bug fix

* refactor: clean code
  • Loading branch information
wtt40122 authored Apr 10, 2024
1 parent 2fc4d94 commit a9d0918
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,48 +84,57 @@ public void reg(String path, Predicate<String> predicate) throws IOException, In
WatchService watchService = FileSystems.getDefault().newWatchService();
directory.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE);
while (true) {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
Path modifiedFile = (Path) event.context();
String filePath = String.format("%s%s", path, modifiedFile.getFileName().toString());
if (!predicate.test(filePath) || modifiedFile.getFileName().toString().startsWith(".")) {
continue;
}
HeraFile hfile = fileMap.get(filePath);

if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
if (null == hfile) {
hfile = initFile(new File(filePath));
}
modify(hfile);
}

if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
fileMap.remove(filePath);
if (null != hfile) {
map.remove(hfile.getFileKey());
listener.onEvent(FileEvent.builder().type(EventType.delete).fileName(filePath).fileKey(hfile.getFileKey()).build());
}
}

if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
File file = new File(filePath);
Object k = FileUtils.fileKey(file);

if (map.containsKey(k)) {
log.info("change name " + map.get(k) + "--->" + file);
listener.onEvent(FileEvent.builder().fileKey(k).type(EventType.rename).build());
} else {
log.info("ENTRY_CREATE filePath:{},fileKey:{}", filePath, k);
HeraFile hf = HeraFile.builder().file(file).fileKey(k).fileName(filePath).build();
map.putIfAbsent(k, hf);
fileMap.put(filePath, hf);

listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build());
try {
WatchKey key = watchService.take();
try {
for (WatchEvent<?> event : key.pollEvents()) {
Path modifiedFile = (Path) event.context();
String filePath = String.format("%s%s", path, modifiedFile.getFileName().toString());
if (!predicate.test(filePath) || modifiedFile.getFileName().toString().startsWith(".")) {
continue;
}
HeraFile hfile = fileMap.get(filePath);

if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
if (null == hfile) {
hfile = initFile(new File(filePath));
}
modify(hfile);
}

if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
fileMap.remove(filePath);
if (null != hfile) {
map.remove(hfile.getFileKey());
listener.onEvent(FileEvent.builder().type(EventType.delete).fileName(filePath).fileKey(hfile.getFileKey()).build());
}
}

if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
File file = new File(filePath);
Object k = FileUtils.fileKey(file);

if (map.containsKey(k)) {
log.info("change name " + map.get(k) + "--->" + file);
listener.onEvent(FileEvent.builder().fileKey(k).type(EventType.rename).build());
} else {
log.info("ENTRY_CREATE filePath:{},fileKey:{}", filePath, k);
HeraFile hf = HeraFile.builder().file(file).fileKey(k).fileName(filePath).build();
map.putIfAbsent(k, hf);
fileMap.put(filePath, hf);

listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build());
}
}
}
} catch (Exception e1) {
log.error("watchService poll events error", e1);
} finally {
key.reset();
}
} catch (Exception e) {
log.error("watchService error", e);
}
key.reset();
}
}

Expand Down Expand Up @@ -165,11 +174,13 @@ private HeraFile initFile(File it) {


private void modify(HeraFile hfile) {
hfile.getUtime().set(System.currentTimeMillis());
if (hfile.getFile().length() == 0) {
listener.onEvent(FileEvent.builder().type(EventType.empty).fileName(hfile.getFileName()).fileKey(hfile.getFileKey()).build());
} else {
listener.onEvent(FileEvent.builder().type(EventType.modify).build());
if (null != hfile) {
hfile.getUtime().set(System.currentTimeMillis());
if (hfile.getFile().length() == 0) {
listener.onEvent(FileEvent.builder().type(EventType.empty).fileName(hfile.getFileName()).fileKey(hfile.getFileKey()).build());
} else {
listener.onEvent(FileEvent.builder().type(EventType.modify).build());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testLogFileMonitor() {

@Test
public void testLogWS() throws IOException {
LogFileWS log = new LogFileWS("D:\\t", new ReadListener() {
LogFile log = new LogFile("D:\\test.log", new ReadListener() {
@Override
public void onEvent(ReadEvent event) {
System.out.println(event.getReadResult().getLines());
Expand Down Expand Up @@ -132,4 +132,5 @@ public void testReadFileCutting() throws IOException {
log.readLine();
System.in.read();
}

}

0 comments on commit a9d0918

Please sign in to comment.