golang-base/internal/config/logging_test.go

104 lines
2.2 KiB
Go

package config
import (
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSetLogLevel(t *testing.T) {
c := New()
for _, i := range []int{0, 21, 41, 61, 81, 101} {
c.LogLevel = i
setLogLevel(&c)
switch i {
case 0:
assert.Equal(t, LevelFatal, c.Log.SLogLevel.Level())
case 21:
assert.Equal(t, slog.LevelError, c.Log.SLogLevel.Level())
case 41:
assert.Equal(t, slog.LevelWarn, c.Log.SLogLevel.Level())
case 61:
assert.Equal(t, slog.LevelInfo, c.Log.SLogLevel.Level())
case 81:
assert.Equal(t, slog.LevelDebug, c.Log.SLogLevel.Level())
case 101:
assert.Equal(t, LevelTrace, c.Log.SLogLevel.Level())
}
}
}
func TestPrintRunningConfig(t *testing.T) {
buf, log := slogToBuffer()
c := New()
c.Log.Log = log
cfgInfo, err := getStructInfo(&c)
assert.NoError(t, err)
printRunningConfig(&c, cfgInfo)
assert.Contains(t, buf.String(), "Running Configuration")
}
func TestFatal(t *testing.T) {
c := New()
buf, log := slogToBuffer()
c.Log.Log = log
c.Log.Fatal("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=ERROR+4")
}
func TestError(t *testing.T) {
c := New()
buf, log := slogToBuffer()
c.Log.Log = log
c.Log.Error("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=ERROR")
}
func TestWarn(t *testing.T) {
c := New()
buf, log := slogToBuffer()
c.Log.Log = log
c.Log.Warn("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=WARN")
}
func TestInfo(t *testing.T) {
c := New()
buf, log := slogToBuffer()
c.Log.Log = log
c.Log.Info("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=INFO")
}
func TestDebug(t *testing.T) {
c := New()
buf, log := slogToBuffer()
c.Log.Log = log
c.Log.Debug("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG")
}
func TestTrace(t *testing.T) {
c := New()
buf, log := slogToBuffer()
c.Log.Log = log
c.Log.Trace("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG-4")
}