From 9fc9af69b40f9704fc3d888c5d0a93caa90a67b1 Mon Sep 17 00:00:00 2001 From: Jude DSouza Date: Thu, 11 Apr 2019 18:04:28 +0200 Subject: [PATCH] Enforce log level filter for log files --- logger/logfile.go | 11 +++++++++++ logger/logfile_test.go | 24 +++++++++++++++++++++--- logger/logger.go | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/logger/logfile.go b/logger/logfile.go index d3ca4749dc..bf13c0955e 100644 --- a/logger/logfile.go +++ b/logger/logfile.go @@ -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 diff --git a/logger/logfile_test.go b/logger/logfile_test.go index 328ec70a70..3f13ac0488 100644 --- a/logger/logfile_test.go +++ b/logger/logfile_test.go @@ -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)) + } +} diff --git a/logger/logger.go b/logger/logger.go index 9ae255a268..8a061936b2 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -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) }