Separates log functions to separate supporting library.

This commit is contained in:
2025-02-14 14:58:40 -06:00
parent a42e1f3e7a
commit a0676c0f54
6 changed files with 221 additions and 206 deletions

141
internal/log/logging.go Normal file
View File

@ -0,0 +1,141 @@
package log
import (
"context"
"log/slog"
"os"
)
const (
LevelTrace = slog.Level(-8)
LevelFatal = slog.Level(12)
)
type Log struct {
Ctx context.Context
Log *slog.Logger
SLogLevel slog.LevelVar
}
var (
// LevelNames set the names associated with custom logging levels.
LevelNames = map[slog.Leveler]string{
LevelTrace: "TRACE",
LevelFatal: "FATAL",
}
// L is the global interface used for calling the logger subfunctions.
L = Log{}
)
func init() {
// Initialize SLog and translate new logging levels
L.Log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: &L.SLogLevel,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
levelLabel, exists := LevelNames[level]
if !exists {
levelLabel = level.String()
}
a.Value = slog.StringValue(levelLabel)
}
return a
},
}))
L.Ctx = context.Background()
}
// SetNumericLevel will set the log level based on a number from 1-100.
// The larger the number the more verbose the logs.
//
// 1-20 = Fatal, 21-40 = Error, 41-60 = Warn, 61-80 = Info, 81-99 = Debug,
// and 100 = Trace.
func SetNumericLevel(level int) {
var llu string = "Log Level Updated"
switch {
// fatal
case level <= 20:
L.SLogLevel.Set(LevelFatal)
L.Info(llu, "level", LevelFatal)
// error
case level > 20 && level <= 40:
L.SLogLevel.Set(slog.LevelError)
L.Info(llu, "level", slog.LevelError)
// warning
case level > 40 && level <= 60:
L.SLogLevel.Set(slog.LevelWarn)
L.Info(llu, "level", slog.LevelWarn)
// info
case level > 60 && level <= 80:
L.SLogLevel.Set(slog.LevelInfo)
L.Info(llu, "level", slog.LevelInfo)
// debug
case level > 80 && level <= 99:
L.SLogLevel.Set(slog.LevelDebug)
L.Info(llu, "level", slog.LevelDebug)
// trace
case level > 99:
L.SLogLevel.Set(LevelTrace)
L.Info(llu, "level", LevelTrace)
}
// set default logger
slog.SetDefault(L.Log)
}
func (log *Log) Fatal(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
LevelFatal,
msg,
attrs...,
)
}
func (log *Log) Error(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
slog.LevelError,
msg,
attrs...,
)
}
func (log *Log) Warn(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
slog.LevelWarn,
msg,
attrs...,
)
}
func (log *Log) Info(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
slog.LevelInfo,
msg,
attrs...,
)
}
func (log *Log) Debug(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
slog.LevelDebug,
msg,
attrs...,
)
}
func (log *Log) Trace(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
LevelTrace,
msg,
attrs...,
)
}

View File

@ -0,0 +1,95 @@
package log
import (
"bytes"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
func slogToBuffer() (*bytes.Buffer, *slog.Logger) {
buf := new(bytes.Buffer)
return buf, slog.New(
slog.NewTextHandler(
buf,
&slog.HandlerOptions{
Level: LevelTrace,
},
),
)
}
func TestSetLogLevel(t *testing.T) {
for _, i := range []int{0, 21, 41, 61, 81, 101} {
SetNumericLevel(i)
switch i {
case 0:
assert.Equal(t, LevelFatal, L.SLogLevel.Level())
case 21:
assert.Equal(t, slog.LevelError, L.SLogLevel.Level())
case 41:
assert.Equal(t, slog.LevelWarn, L.SLogLevel.Level())
case 61:
assert.Equal(t, slog.LevelInfo, L.SLogLevel.Level())
case 81:
assert.Equal(t, slog.LevelDebug, L.SLogLevel.Level())
case 101:
assert.Equal(t, LevelTrace, L.SLogLevel.Level())
}
}
}
func TestFatal(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Fatal("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=ERROR+4")
}
func TestError(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Error("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=ERROR")
}
func TestWarn(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Warn("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=WARN")
}
func TestInfo(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Info("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=INFO")
}
func TestDebug(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Debug("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG")
}
func TestTrace(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Trace("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG-4")
}