simplifies calls to log functions.

This commit is contained in:
Hyatt 2025-02-14 15:05:08 -06:00
parent a0676c0f54
commit eb45137776
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA
4 changed files with 40 additions and 40 deletions

View File

@ -1,11 +1,11 @@
package config
import (
"log"
l "log"
"os"
"time"
l "example.com/golang-base/internal/log"
"example.com/golang-base/internal/log"
)
func Init() Config {
@ -14,22 +14,22 @@ func Init() Config {
// parse config structure
cfgInfo, err := getStructInfo(&cfg)
if err != nil {
log.Fatalf("Unable to initialize program: %v", err)
l.Fatalf("Unable to initialize program: %v", err)
}
// get command line flags
if err := cfg.parseFlags(cfgInfo); err != nil {
log.Fatalf("Unable to initialize program: %v", err)
l.Fatalf("Unable to initialize program: %v", err)
}
// set logging Level
l.SetNumericLevel(cfg.LogLevel)
log.SetNumericLevel(cfg.LogLevel)
// set timezone & time format
cfg.TZUTC, _ = time.LoadLocation("UTC")
cfg.TZLocal, err = time.LoadLocation(cfg.TimeZoneLocal)
if err != nil {
l.L.Error("Unable to parse timezone string", "error", err)
log.Error("Unable to parse timezone string", "error", err)
os.Exit(1)
}

View File

@ -36,18 +36,18 @@ func printRunningConfig(cfg *Config, cfgInfo []structInfo) {
for _, info := range cfgInfo {
if info.Secret {
log.L.Debug(logRunningConfiguration, info.Name, "REDACTED")
log.Debug(logRunningConfiguration, info.Name, "REDACTED")
} else {
switch info.Type.String() {
case "string":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*string)
log.L.Debug(logRunningConfiguration, info.Alt, *p)
log.Debug(logRunningConfiguration, info.Alt, *p)
case "bool":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*bool)
log.L.Log.Debug(logRunningConfiguration, info.Alt, strconv.FormatBool(*p))
log.Debug(logRunningConfiguration, info.Alt, strconv.FormatBool(*p))
case "int":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*int)
log.L.Log.Debug(logRunningConfiguration, info.Alt, strconv.FormatInt(int64(*p), 10))
log.Debug(logRunningConfiguration, info.Alt, strconv.FormatInt(int64(*p), 10))
}
}
}

View File

@ -59,81 +59,81 @@ func SetNumericLevel(level int) {
// fatal
case level <= 20:
L.SLogLevel.Set(LevelFatal)
L.Info(llu, "level", LevelFatal)
Info(llu, "level", LevelFatal)
// error
case level > 20 && level <= 40:
L.SLogLevel.Set(slog.LevelError)
L.Info(llu, "level", slog.LevelError)
Info(llu, "level", slog.LevelError)
// warning
case level > 40 && level <= 60:
L.SLogLevel.Set(slog.LevelWarn)
L.Info(llu, "level", slog.LevelWarn)
Info(llu, "level", slog.LevelWarn)
// info
case level > 60 && level <= 80:
L.SLogLevel.Set(slog.LevelInfo)
L.Info(llu, "level", slog.LevelInfo)
Info(llu, "level", slog.LevelInfo)
// debug
case level > 80 && level <= 99:
L.SLogLevel.Set(slog.LevelDebug)
L.Info(llu, "level", slog.LevelDebug)
Info(llu, "level", slog.LevelDebug)
// trace
case level > 99:
L.SLogLevel.Set(LevelTrace)
L.Info(llu, "level", LevelTrace)
Info(llu, "level", LevelTrace)
}
// set default logger
slog.SetDefault(L.Log)
}
func (log *Log) Fatal(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
func Fatal(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
LevelFatal,
msg,
attrs...,
)
}
func (log *Log) Error(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
func Error(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelError,
msg,
attrs...,
)
}
func (log *Log) Warn(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
func Warn(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelWarn,
msg,
attrs...,
)
}
func (log *Log) Info(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
func Info(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelInfo,
msg,
attrs...,
)
}
func (log *Log) Debug(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
func Debug(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelDebug,
msg,
attrs...,
)
}
func (log *Log) Trace(msg string, attrs ...interface{}) {
log.Log.Log(
log.Ctx,
func Trace(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
LevelTrace,
msg,
attrs...,

View File

@ -44,7 +44,7 @@ func TestFatal(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Fatal("TEST Message")
Fatal("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=ERROR+4")
}
@ -53,7 +53,7 @@ func TestError(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Error("TEST Message")
Error("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=ERROR")
}
@ -62,7 +62,7 @@ func TestWarn(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Warn("TEST Message")
Warn("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=WARN")
}
@ -71,7 +71,7 @@ func TestInfo(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Info("TEST Message")
Info("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=INFO")
}
@ -80,7 +80,7 @@ func TestDebug(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Debug("TEST Message")
Debug("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG")
}
@ -89,7 +89,7 @@ func TestTrace(t *testing.T) {
buf, log := slogToBuffer()
L.Log = log
L.Trace("TEST Message")
Trace("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG-4")
}