(new): add threshold-based log level filtering and docs
Golang lint / lint (push) Successful in 1m36s
Golang lint / lint (push) Successful in 1m36s
- add threshold-aware LogLevel constructors - add Logger.SetThresholdMode and SameThreshold - preserve legacy SameLevel behavior for compatibility - extend tests for threshold mode and compatibility - update README and release notes for v2.2.0
This commit is contained in:
+83
-8
@@ -120,6 +120,77 @@ func TestLoggerPrintfFormatsMessage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggerUsesSeverityOrderingWhenThresholdModeIsDisabled(t *testing.T) {
|
||||
writer := &stubLoggerWriter{}
|
||||
logger := CreateLogger().
|
||||
SetLevel(NewThresholdLogLevel(1, 0, "warn")).
|
||||
AddWriter(writer)
|
||||
|
||||
logger.Print(NewThresholdLogLevel(0, 100, "info"))
|
||||
logger.Print(NewThresholdLogLevel(2, 0, "error"))
|
||||
|
||||
if writer.printCalls != 1 {
|
||||
t.Fatalf("severity mode should allow only lower-or-equal severity levels, got %d writes", writer.printCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoggerUsesThresholdOrderingWhenThresholdModeIsEnabled(t *testing.T) {
|
||||
writer := &stubLoggerWriter{}
|
||||
logger := CreateLogger().
|
||||
SetLevel(NewThresholdLogLevel(4, 20, "custom")).
|
||||
SetThresholdMode(true).
|
||||
AddWriter(writer)
|
||||
|
||||
logger.Print(NewThresholdLogLevel(4, 10, "debug"))
|
||||
logger.Print(NewThresholdLogLevel(0, 30, "info"))
|
||||
|
||||
if writer.printCalls != 1 {
|
||||
t.Fatalf("threshold mode should allow only entries at or below the configured threshold, got %d writes", writer.printCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeprecatedLevelMethodKeepsLegacySeverityBehavior(t *testing.T) {
|
||||
writer := &stubLoggerWriter{}
|
||||
logger := CreateLogger().
|
||||
Level(FATAL).
|
||||
AddWriter(writer)
|
||||
|
||||
logger.Print(INFO, "info")
|
||||
logger.Print(WARN, "warn")
|
||||
logger.Print(ERROR, "error")
|
||||
logger.Print(FATAL, "fatal")
|
||||
logger.Print(DEBUG, "debug")
|
||||
|
||||
if writer.printCalls != 4 {
|
||||
t.Fatalf("Level(FATAL) should keep legacy behavior and allow INFO/WARN/ERROR/FATAL only, got %d writes", writer.printCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameLevelIgnoresThresholdDifferencesForCompatibility(t *testing.T) {
|
||||
legacy := NewLogLevel(1, "warn")
|
||||
threshold := NewThresholdLogLevel(1, 20, "warn")
|
||||
|
||||
if !legacy.SameLevel(threshold) {
|
||||
t.Fatalf("SameLevel() should continue comparing only severity index")
|
||||
}
|
||||
if legacy.Equal(threshold) {
|
||||
t.Fatalf("Equal() should distinguish levels with different threshold configuration")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameThresholdComparesOnlyThresholdValue(t *testing.T) {
|
||||
first := NewThresholdLogLevel(1, 20, "warn")
|
||||
second := NewThresholdLogLevel(9, 20, "custom")
|
||||
third := NewThresholdLogLevel(1, 30, "warn")
|
||||
|
||||
if !first.SameThreshold(second) {
|
||||
t.Fatalf("SameThreshold() should ignore severity and compare threshold only")
|
||||
}
|
||||
if first.SameThreshold(third) {
|
||||
t.Fatalf("SameThreshold() should detect different threshold values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogLevelForMethodReturnsExpectedLevels(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -150,15 +221,16 @@ func TestLogLevelForMethodReturnsExpectedLevels(t *testing.T) {
|
||||
|
||||
func TestNewLevelConstructorsSetExpectedSeverity(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
got LogLevel
|
||||
want LogLevel
|
||||
name string
|
||||
got LogLevel
|
||||
want LogLevel
|
||||
wantTh uint8
|
||||
}{
|
||||
{name: "info", got: NewInfoLogLevel("custom"), want: INFO},
|
||||
{name: "warn", got: NewWarnLogLevel("custom"), want: WARN},
|
||||
{name: "error", got: NewErrorLogLevel("custom"), want: ERROR},
|
||||
{name: "fatal", got: NewFatalLogLevel("custom"), want: FATAL},
|
||||
{name: "debug", got: NewDebugLogLevel("custom"), want: DEBUG},
|
||||
{name: "info", got: NewInfoLogLevel("custom"), want: INFO, wantTh: 10},
|
||||
{name: "warn", got: NewWarnLogLevel("custom"), want: WARN, wantTh: 20},
|
||||
{name: "error", got: NewErrorLogLevel("custom"), want: ERROR, wantTh: 30},
|
||||
{name: "fatal", got: NewFatalLogLevel("custom"), want: FATAL, wantTh: 40},
|
||||
{name: "debug", got: NewDebugLogLevel("custom"), want: DEBUG, wantTh: 0},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -166,6 +238,9 @@ func TestNewLevelConstructorsSetExpectedSeverity(t *testing.T) {
|
||||
if tt.got.n != tt.want.n {
|
||||
t.Fatalf("%s severity = %d, want %d", tt.name, tt.got.n, tt.want.n)
|
||||
}
|
||||
if tt.got.th != tt.wantTh {
|
||||
t.Fatalf("%s threshold = %d, want %d", tt.name, tt.got.th, tt.wantTh)
|
||||
}
|
||||
if tt.got.GetName() != "custom" {
|
||||
t.Fatalf("%s constructor should preserve name, got %q", tt.name, tt.got.GetName())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user