Browse Source

mlog: update for upstream rotatelogs API change

Since we're ignoring the error, I've also added a test to ensure the
error probably isn't going to happen.

A panic() "might" makes sense here, but I'd rather not for now.
Damian Gryski 1 year ago
parent
commit
e65d7f80b5
2 changed files with 22 additions and 2 deletions
  1. 4
    2
      mlog/minilog.go
  2. 18
    0
      mlog/minilog_test.go

+ 4
- 2
mlog/minilog.go View File

@@ -28,9 +28,11 @@ func GetOutput() io.Writer {
28 28
 	return output
29 29
 }
30 30
 
31
+const strftimeFormat = ".%Y%m%d%H%M.log"
32
+
31 33
 func SetOutput(logdir, service string, stdout bool) {
32
-	rl := rotatelogs.New(
33
-		logdir+"/"+service+".%Y%m%d%H%M.log",
34
+	rl, _ := rotatelogs.New(
35
+		logdir+"/"+service+strftimeFormat,
34 36
 		rotatelogs.WithClock(rotatelogs.Local),
35 37
 		rotatelogs.WithLinkName(logdir+"/"+service+".log"),
36 38
 	)

+ 18
- 0
mlog/minilog_test.go View File

@@ -4,6 +4,8 @@ import (
4 4
 	"bytes"
5 5
 	"log"
6 6
 	"testing"
7
+
8
+	strftime "github.com/lestrrat/go-strftime"
7 9
 )
8 10
 
9 11
 func TestGetOutput(t *testing.T) {
@@ -30,3 +32,19 @@ func TestSetRawStream(t *testing.T) {
30 32
 		t.Errorf("actual log '%s' is different from the expected '%s'", actualLog, expectedLog)
31 33
 	}
32 34
 }
35
+
36
+func TestStrftime(t *testing.T) {
37
+	// Since we're ignoring the error from rotatelogs.New(), we may as well
38
+	// assert that the only error it's likely to give us (a strftime error)
39
+	// isn't likely to happen.
40
+	// This isn't perfect, but it's better than nothing.
41
+
42
+	pattern := "/var/log/carbonzipper" + strftimeFormat
43
+
44
+	_, err := strftime.New(pattern)
45
+
46
+	if err != nil {
47
+		t.Errorf("strftime.New(%q) returned an error: %v", pattern, err)
48
+	}
49
+
50
+}