75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package applog
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseLevel(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
expected Level
|
|
}{
|
|
{"debug", LevelDebug},
|
|
{"DEBUG", LevelDebug},
|
|
{"info", LevelInfo},
|
|
{"INFO", LevelInfo},
|
|
{"warn", LevelWarn},
|
|
{"warning", LevelWarn},
|
|
{"WARN", LevelWarn},
|
|
{"error", LevelError},
|
|
{"ERROR", LevelError},
|
|
{"unknown", LevelInfo},
|
|
{"", LevelInfo},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
got := ParseLevel(tc.input)
|
|
if got != tc.expected {
|
|
t.Errorf("ParseLevel(%q) = %v, want %v", tc.input, got, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLogLevelFiltering(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
log.SetOutput(&buf)
|
|
defer log.SetOutput(log.Writer())
|
|
|
|
SetLevel("warn")
|
|
|
|
buf.Reset()
|
|
Debugf("this is debug: %d", 1)
|
|
if buf.Len() > 0 {
|
|
t.Errorf("expected debug to be filtered out, got %q", buf.String())
|
|
}
|
|
|
|
buf.Reset()
|
|
Infof("this is info: %d", 2)
|
|
if buf.Len() > 0 {
|
|
t.Errorf("expected info to be filtered out, got %q", buf.String())
|
|
}
|
|
|
|
buf.Reset()
|
|
Warnf("this is warn: %d", 3)
|
|
if !strings.Contains(buf.String(), "[WARN] this is warn: 3") {
|
|
t.Errorf("expected warn to be logged, got %q", buf.String())
|
|
}
|
|
|
|
buf.Reset()
|
|
Errorf("this is error: %d", 4)
|
|
if !strings.Contains(buf.String(), "[ERROR] this is error: 4") {
|
|
t.Errorf("expected error to be logged, got %q", buf.String())
|
|
}
|
|
|
|
// Now switch to debug
|
|
SetLevel("debug")
|
|
buf.Reset()
|
|
Debugf("debug now visible")
|
|
if !strings.Contains(buf.String(), "[DEBUG] debug now visible") {
|
|
t.Errorf("expected debug to be logged after SetLevel(debug), got %q", buf.String())
|
|
}
|
|
}
|