summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-core/src/main/java/com/yahoo/container/handler/LogReader.java')
-rw-r--r--container-core/src/main/java/com/yahoo/container/handler/LogReader.java60
1 files changed, 39 insertions, 21 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
index c6ae20973fb..df289bd906f 100644
--- a/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
+++ b/container-core/src/main/java/com/yahoo/container/handler/LogReader.java
@@ -1,40 +1,58 @@
// 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 com.yahoo.vespa.defaults.Defaults;
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;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Base64;
+import java.util.Iterator;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+class LogReader {
-public class LogReader {
+ private final Path logDirectory;
+ private final Pattern logFilePattern;
- long earliestLogThreshold;
- long latestLogThreshold;
+ LogReader(String logDirectory, String logFilePattern) {
+ this(Paths.get(Defaults.getDefaults().underVespaHome(logDirectory)), Pattern.compile(logFilePattern));
+ }
+
+ LogReader(Path logDirectory, Pattern logFilePattern) {
+ this.logDirectory = logDirectory;
+ this.logFilePattern = logFilePattern;
+ }
- 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 readLogs(Instant earliestLogThreshold, Instant latestLogThreshold) throws IOException, JSONException {
JSONObject json = new JSONObject();
- File root = new File(logDirectory);
- traverse_folder(root, json, "");
+ latestLogThreshold = latestLogThreshold.plus(Duration.ofMinutes(5)); // Add some time to allow retrieving logs currently being modified
+ traverseFolder(logDirectory, json, earliestLogThreshold, latestLogThreshold, "");
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() + "-");
+ private void traverseFolder(Path path, JSONObject json, Instant earliestLogThreshold, Instant latestLogThreshold, String filenamePrefix) throws IOException, JSONException {
+ try (Stream<Path> files = Files.list(path)) {
+ for (Iterator<Path> it = files.iterator(); it.hasNext(); ) {
+ Path child = it.next();
+ String filename = child.getFileName().toString();
+ Instant lastModified = Files.getLastModifiedTime(child).toInstant();
+ if (Files.isRegularFile(child)) {
+ if (lastModified.isAfter(earliestLogThreshold) &&
+ lastModified.isBefore(latestLogThreshold) &&
+ logFilePattern.matcher(filename).matches()) {
+ json.put(filenamePrefix + filename, Base64.getEncoder().encodeToString(Files.readAllBytes(child)));
+ }
+ } else {
+ traverseFolder(child, json, earliestLogThreshold, latestLogThreshold, filenamePrefix + filename + "-");
+ }
}
}
}
-
}