|
|
|
@ -7,6 +7,7 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
|
"github.com/hashicorp/logutils"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
@ -19,7 +20,8 @@ func TestLogFile_timeRotation(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterTime")
|
|
|
|
|
defer os.Remove(tempDir)
|
|
|
|
|
logFile := LogFile{fileName: testFileName, logPath: tempDir, duration: testDuration}
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
|
logFile := LogFile{logFilter: filt, fileName: testFileName, logPath: tempDir, duration: testDuration}
|
|
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
logFile.Write([]byte("Second File"))
|
|
|
|
@ -34,7 +36,6 @@ func TestLogFile_openNew(t *testing.T) {
|
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterOpen")
|
|
|
|
|
defer os.Remove(tempDir)
|
|
|
|
|
logFile := LogFile{fileName: testFileName, logPath: tempDir, duration: testDuration}
|
|
|
|
|
|
|
|
|
|
if err := logFile.openNew(); err != nil {
|
|
|
|
|
t.Errorf("Expected open file %s, got an error (%s)", testFileName, err)
|
|
|
|
|
}
|
|
|
|
@ -48,7 +49,9 @@ func TestLogFile_byteRotation(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterBytes")
|
|
|
|
|
defer os.Remove(tempDir)
|
|
|
|
|
logFile := LogFile{fileName: testFileName, logPath: tempDir, MaxBytes: testBytes, duration: 24 * time.Hour}
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
|
filt.MinLevel = logutils.LogLevel("INFO")
|
|
|
|
|
logFile := LogFile{logFilter: filt, fileName: testFileName, logPath: tempDir, MaxBytes: testBytes, duration: 24 * time.Hour}
|
|
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
|
logFile.Write([]byte("Second File"))
|
|
|
|
|
want := 2
|
|
|
|
@ -57,3 +60,18 @@ func TestLogFile_byteRotation(t *testing.T) {
|
|
|
|
|
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLogFile_logLevelFiltering(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterTime")
|
|
|
|
|
defer os.Remove(tempDir)
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
|
logFile := LogFile{logFilter: filt, fileName: testFileName, logPath: tempDir, MaxBytes: testBytes, duration: testDuration}
|
|
|
|
|
logFile.Write([]byte("[INFO] This is an info message"))
|
|
|
|
|
logFile.Write([]byte("[DEBUG] This is a debug message"))
|
|
|
|
|
logFile.Write([]byte("[ERR] This is an error message"))
|
|
|
|
|
want := 2
|
|
|
|
|
if got, _ := ioutil.ReadDir(tempDir); len(got) != want {
|
|
|
|
|
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|