blob: c6ae20973fbae27fbb1655839aac3d6dfb892690 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;
import org.json.JSONException;
import org.json.JSONObject;
import java.time.Duration;
import java.util.Base64;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class LogReader {
long earliestLogThreshold;
long latestLogThreshold;
protected JSONObject readLogs(String logDirectory, long earliestLogThreshold, long latestLogThreshold) throws IOException, JSONException {
this.earliestLogThreshold = earliestLogThreshold;
this.latestLogThreshold = latestLogThreshold + Duration.ofMinutes(5).toMillis(); // Add some time to allow retrieving logs currently being modified
JSONObject json = new JSONObject();
File root = new File(logDirectory);
traverse_folder(root, json, "");
return json;
}
private void traverse_folder(File root, JSONObject json, String filename) throws IOException, JSONException {
File[] files = root.listFiles();
for(File child : files) {
long logTime = Files.getLastModifiedTime(child.toPath()).toMillis();
if(child.isFile() && earliestLogThreshold < logTime && logTime < latestLogThreshold) {
json.put(filename + child.getName(), Base64.getEncoder().encodeToString(Files.readAllBytes(child.toPath())));
}
else if (!child.isFile()){
traverse_folder(child, json, filename + child.getName() + "-");
}
}
}
}
|