Enforce log level filter for log files

This commit is contained in:
Jude DSouza 2019-04-11 18:04:28 +02:00 committed by Freddy
parent 31a234b443
commit 9fc9af69b4
3 changed files with 33 additions and 4 deletions

View File

@ -7,6 +7,8 @@ import (
"strings"
"sync"
"time"
"github.com/hashicorp/logutils"
)
var (
@ -15,6 +17,9 @@ var (
//LogFile is used to setup a file based logger that also performs log rotation
type LogFile struct {
// Log level Filter to filter out logs that do not matcch LogLevel criteria
logFilter *logutils.LevelFilter
//Name of the log file
fileName string
@ -76,7 +81,13 @@ func (l *LogFile) rotate() error {
return nil
}
// Write is used to implement io.Writer
func (l *LogFile) Write(b []byte) (n int, err error) {
// Filter out log entries that do not match log level criteria
if !l.logFilter.Check(b) {
return 0, nil
}
l.acquire.Lock()
defer l.acquire.Unlock()
//Create a new file if we have no file to write to

View File

@ -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))
}
}

View File

@ -123,7 +123,7 @@ func Setup(config *Config, ui cli.Ui) (*logutils.LevelFilter, *GatedWriter, *Log
if config.LogRotateBytes != 0 {
logRotateBytes = config.LogRotateBytes
}
logFile := &LogFile{fileName: fileName, logPath: dir, duration: logRotateDuration, MaxBytes: logRotateBytes}
logFile := &LogFile{logFilter: logFilter, fileName: fileName, logPath: dir, duration: logRotateDuration, MaxBytes: logRotateBytes}
writers = append(writers, logFile)
}