core/logger/logger_test.go
Zeni Kim 0da7ea8ab1 Added Level type - A custom int type with constants DEBUG, INFO, WARNING, ERROR in increasing severity order.
Added `minLevel` field to Logger struct to track the minimum level threshold.
Added `SetLevel()` and `GetLevel()` methods for dynamically changing/reading the log level at runtime.
2026-05-06 16:06:25 -05:00

300 lines
7.4 KiB
Go

package logger
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/uuid"
)
func TestNewLogger(t *testing.T) {
fp := filepath.Join(t.TempDir(), uuid.NewString())
f, err := os.Create(fp)
if err != nil {
t.Errorf("failed test new logger")
}
f.Close()
l := NewLogger(&LogNullDriver{})
l.Info("testing")
fdrv := &LogFileDriver{
fp,
}
trgt := fdrv.GetTarget()
ts, ok := trgt.(string)
if !ok {
t.Errorf("failed test new logger")
}
if ts != fp {
t.Errorf("failed test new logger")
}
l = NewLogger(fdrv)
l.Error("test-err")
f, err = os.Open(fp)
if err != nil {
t.Errorf("failed test new logger")
}
defer f.Close()
b, err := io.ReadAll(f)
if !strings.Contains(string(b), "test-err") {
t.Errorf("failed test new logger")
}
}
func TestInfo(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLogger(&LogFileDriver{
FilePath: path,
})
l.Info("DFT2V56H")
lf, err := os.Open(path)
if err != nil {
t.Error("failed testing info")
}
d, err := io.ReadAll(lf)
if err != nil {
t.Error("error testing info")
}
if !strings.Contains(string(d), "DFT2V56H") {
t.Error("error testing info")
}
t.Cleanup(func() {
l.Close()
})
}
func TestWarning(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLogger(&LogFileDriver{
FilePath: path,
})
l.Warning("DFT2V56H")
lf, err := os.Open(path)
if err != nil {
t.Error("failed testing warning")
}
d, err := io.ReadAll(lf)
if err != nil {
t.Error("failed testing warning")
}
if !strings.Contains(string(d), "DFT2V56H") {
t.Error("failed testing warning")
}
t.Cleanup(func() {
l.Close()
})
}
func TestDebug(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLogger(&LogFileDriver{
FilePath: path,
})
l.Debug("DFT2V56H")
lf, err := os.Open(path)
if err != nil {
t.Error("failed testing debug")
}
d, err := io.ReadAll(lf)
if err != nil {
t.Error("error testing debug")
}
if !strings.Contains(string(d), "DFT2V56H") {
t.Error("error testing debug")
}
t.Cleanup(func() {
l.Close()
})
}
func TestError(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLogger(&LogFileDriver{
FilePath: path,
})
l.Error("DFT2V56H")
lf, err := os.Open(path)
if err != nil {
t.Error("failed testing error")
}
d, err := io.ReadAll(lf)
if err != nil {
t.Error("failed testing error")
}
if !strings.Contains(string(d), "DFT2V56H") {
t.Error("failed testing error")
}
t.Cleanup(func() {
l.Close()
})
}
func TestLevelFiltering(t *testing.T) {
t.Run("NewLoggerWithLevel_DEBUG", func(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLoggerWithLevel(&LogFileDriver{FilePath: path}, DEBUG)
l.Debug("debug-msg")
l.Info("info-msg")
l.Warning("warn-msg")
l.Error("err-msg")
l.Close()
b, _ := os.ReadFile(path)
content := string(b)
if !strings.Contains(content, "debug-msg") {
t.Error("expected debug-msg to be logged at DEBUG level")
}
if !strings.Contains(content, "info-msg") {
t.Error("expected info-msg to be logged at DEBUG level")
}
if !strings.Contains(content, "warn-msg") {
t.Error("expected warn-msg to be logged at DEBUG level")
}
if !strings.Contains(content, "err-msg") {
t.Error("expected err-msg to be logged at DEBUG level")
}
})
t.Run("NewLoggerWithLevel_INFO", func(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLoggerWithLevel(&LogFileDriver{FilePath: path}, INFO)
l.Debug("debug-msg")
l.Info("info-msg")
l.Warning("warn-msg")
l.Error("err-msg")
l.Close()
b, _ := os.ReadFile(path)
content := string(b)
if strings.Contains(content, "debug-msg") {
t.Error("debug-msg should NOT be logged at INFO minimum level")
}
if !strings.Contains(content, "info-msg") {
t.Error("expected info-msg to be logged at INFO level")
}
if !strings.Contains(content, "warn-msg") {
t.Error("expected warn-msg to be logged at INFO level")
}
if !strings.Contains(content, "err-msg") {
t.Error("expected err-msg to be logged at INFO level")
}
})
t.Run("NewLoggerWithLevel_WARNING", func(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLoggerWithLevel(&LogFileDriver{FilePath: path}, WARNING)
l.Debug("debug-msg")
l.Info("info-msg")
l.Warning("warn-msg")
l.Error("err-msg")
l.Close()
b, _ := os.ReadFile(path)
content := string(b)
if strings.Contains(content, "debug-msg") {
t.Error("debug-msg should NOT be logged at WARNING minimum level")
}
if strings.Contains(content, "info-msg") {
t.Error("info-msg should NOT be logged at WARNING minimum level")
}
if !strings.Contains(content, "warn-msg") {
t.Error("expected warn-msg to be logged at WARNING level")
}
if !strings.Contains(content, "err-msg") {
t.Error("expected err-msg to be logged at WARNING level")
}
})
t.Run("NewLoggerWithLevel_ERROR", func(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLoggerWithLevel(&LogFileDriver{FilePath: path}, ERROR)
l.Debug("debug-msg")
l.Info("info-msg")
l.Warning("warn-msg")
l.Error("err-msg")
l.Close()
b, _ := os.ReadFile(path)
content := string(b)
if strings.Contains(content, "debug-msg") {
t.Error("debug-msg should NOT be logged at ERROR minimum level")
}
if strings.Contains(content, "info-msg") {
t.Error("info-msg should NOT be logged at ERROR minimum level")
}
if strings.Contains(content, "warn-msg") {
t.Error("warn-msg should NOT be logged at ERROR minimum level")
}
if !strings.Contains(content, "err-msg") {
t.Error("expected err-msg to be logged at ERROR level")
}
})
t.Run("SetLevel_dynamically", func(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLogger(&LogFileDriver{FilePath: path})
// Default is DEBUG, so debug messages are logged
l.Debug("debug-msg-before")
l.SetLevel(WARNING)
l.Debug("debug-msg-after")
l.Info("info-msg-after")
l.Warning("warn-msg-after")
l.Error("err-msg-after")
l.Close()
b, _ := os.ReadFile(path)
content := string(b)
if !strings.Contains(content, "debug-msg-before") {
t.Error("expected debug-msg-before to be logged before level change")
}
if strings.Contains(content, "debug-msg-after") {
t.Error("debug-msg-after should NOT be logged after setting level to WARNING")
}
if strings.Contains(content, "info-msg-after") {
t.Error("info-msg-after should NOT be logged after setting level to WARNING")
}
if !strings.Contains(content, "warn-msg-after") {
t.Error("expected warn-msg-after to be logged at WARNING level")
}
if !strings.Contains(content, "err-msg-after") {
t.Error("expected err-msg-after to be logged at WARNING level")
}
})
}
func TestLevelString(t *testing.T) {
tests := []struct {
level Level
want string
}{
{DEBUG, "debug"},
{INFO, "info"},
{WARNING, "warning"},
{ERROR, "error"},
{Level(99), "unknown"},
}
for _, tt := range tests {
if got := tt.level.String(); got != tt.want {
t.Errorf("Level(%d).String() = %q, want %q", tt.level, got, tt.want)
}
}
}
func TestGetLevel(t *testing.T) {
path := filepath.Join(t.TempDir(), uuid.NewString())
l := NewLoggerWithLevel(&LogFileDriver{FilePath: path}, WARNING)
defer l.Close()
if l.GetLevel() != WARNING {
t.Errorf("GetLevel() = %d, want %d", l.GetLevel(), WARNING)
}
l.SetLevel(INFO)
if l.GetLevel() != INFO {
t.Errorf("GetLevel() after SetLevel(INFO) = %d, want %d", l.GetLevel(), INFO)
}
}