summaryrefslogtreecommitdiffstats
path: root/vespalog/src/main/java/com/yahoo/log/LogFileDb.java
diff options
context:
space:
mode:
authorArne Juul <arnej@yahoo-inc.com>2018-09-03 08:00:12 +0000
committerArne Juul <arnej@yahoo-inc.com>2018-09-06 13:00:14 +0000
commit30fd1322c666d8e48fae340dc69ce5030069e30b (patch)
treeee2b340ea309ff07d8bc867952dc65abed863f06 /vespalog/src/main/java/com/yahoo/log/LogFileDb.java
parentae594f2b7453ff1c5109fd8a9cec9339ec0a6366 (diff)
enforce log retention policies
* for access logs, save meta-data about the log file itself in a simple format. * implement a proof-of-concept shell script that removes log files after one month. * ensure retention enforcer is started when services start * note that retention enforcer will continue running even after services stop, but it has protection to ensure that it won't multiply endlessly.
Diffstat (limited to 'vespalog/src/main/java/com/yahoo/log/LogFileDb.java')
-rw-r--r--vespalog/src/main/java/com/yahoo/log/LogFileDb.java50
1 files changed, 50 insertions, 0 deletions
diff --git a/vespalog/src/main/java/com/yahoo/log/LogFileDb.java b/vespalog/src/main/java/com/yahoo/log/LogFileDb.java
new file mode 100644
index 00000000000..d0fa64805bf
--- /dev/null
+++ b/vespalog/src/main/java/com/yahoo/log/LogFileDb.java
@@ -0,0 +1,50 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.log;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.nio.file.StandardOpenOption.*;
+
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static com.yahoo.vespa.defaults.Defaults.getDefaults;
+
+
+/**
+ * @author arnej
+ *
+ * This class takes care of saving meta-data about a log-file,
+ * ensuring that we can enact policies about log retention.
+ **/
+public class LogFileDb {
+
+ static final String DBDIR = "var/db/vespa/logfiledb/";
+
+ private static long dayStamp() {
+ long s = System.currentTimeMillis() / 1000;
+ return s / 100000;
+ }
+
+ private static OutputStream metaFile() throws java.io.IOException {
+ String fn = getDefaults().underVespaHome(DBDIR + "logfiles." + dayStamp());
+ Path path = Paths.get(fn);
+ return Files.newOutputStream(path, CREATE, APPEND);
+ }
+
+ public static void nowLoggingTo(String filename) {
+ if (filename.contains("\n")) {
+ throw new IllegalArgumentException("Cannot use filename with newline: "+filename);
+ }
+ long s = System.currentTimeMillis() / 1000;
+ String meta = "" + s + " " + filename + "\n";
+ byte[] data = meta.getBytes(UTF_8);
+ try (OutputStream out = metaFile()) {
+ out.write(data);
+ } catch (java.io.IOException e) {
+ System.err.println("Saving meta-data about logfile "+filename+" failed: "+e);
+ // ignore
+ }
+ }
+}