mirror of https://github.com/status-im/consul.git
Enforce log level filter for log files
This commit is contained in:
parent
31a234b443
commit
9fc9af69b4
|
@ -7,6 +7,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -15,6 +17,9 @@ var (
|
||||||
|
|
||||||
//LogFile is used to setup a file based logger that also performs log rotation
|
//LogFile is used to setup a file based logger that also performs log rotation
|
||||||
type LogFile struct {
|
type LogFile struct {
|
||||||
|
// Log level Filter to filter out logs that do not matcch LogLevel criteria
|
||||||
|
logFilter *logutils.LevelFilter
|
||||||
|
|
||||||
//Name of the log file
|
//Name of the log file
|
||||||
fileName string
|
fileName string
|
||||||
|
|
||||||
|
@ -76,7 +81,13 @@ func (l *LogFile) rotate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write is used to implement io.Writer
|
||||||
func (l *LogFile) Write(b []byte) (n int, err error) {
|
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()
|
l.acquire.Lock()
|
||||||
defer l.acquire.Unlock()
|
defer l.acquire.Unlock()
|
||||||
//Create a new file if we have no file to write to
|
//Create a new file if we have no file to write to
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -19,7 +20,8 @@ func TestLogFile_timeRotation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
tempDir := testutil.TempDir(t, "LogWriterTime")
|
tempDir := testutil.TempDir(t, "LogWriterTime")
|
||||||
defer os.Remove(tempDir)
|
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"))
|
logFile.Write([]byte("Hello World"))
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
logFile.Write([]byte("Second File"))
|
logFile.Write([]byte("Second File"))
|
||||||
|
@ -34,7 +36,6 @@ func TestLogFile_openNew(t *testing.T) {
|
||||||
tempDir := testutil.TempDir(t, "LogWriterOpen")
|
tempDir := testutil.TempDir(t, "LogWriterOpen")
|
||||||
defer os.Remove(tempDir)
|
defer os.Remove(tempDir)
|
||||||
logFile := LogFile{fileName: testFileName, logPath: tempDir, duration: testDuration}
|
logFile := LogFile{fileName: testFileName, logPath: tempDir, duration: testDuration}
|
||||||
|
|
||||||
if err := logFile.openNew(); err != nil {
|
if err := logFile.openNew(); err != nil {
|
||||||
t.Errorf("Expected open file %s, got an error (%s)", testFileName, err)
|
t.Errorf("Expected open file %s, got an error (%s)", testFileName, err)
|
||||||
}
|
}
|
||||||
|
@ -48,7 +49,9 @@ func TestLogFile_byteRotation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
tempDir := testutil.TempDir(t, "LogWriterBytes")
|
tempDir := testutil.TempDir(t, "LogWriterBytes")
|
||||||
defer os.Remove(tempDir)
|
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("Hello World"))
|
||||||
logFile.Write([]byte("Second File"))
|
logFile.Write([]byte("Second File"))
|
||||||
want := 2
|
want := 2
|
||||||
|
@ -57,3 +60,18 @@ func TestLogFile_byteRotation(t *testing.T) {
|
||||||
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -123,7 +123,7 @@ func Setup(config *Config, ui cli.Ui) (*logutils.LevelFilter, *GatedWriter, *Log
|
||||||
if config.LogRotateBytes != 0 {
|
if config.LogRotateBytes != 0 {
|
||||||
logRotateBytes = config.LogRotateBytes
|
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)
|
writers = append(writers, logFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue