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

View File

@ -36,18 +36,18 @@ func printRunningConfig(cfg *Config, cfgInfo []structInfo) {
for _, info := range cfgInfo { for _, info := range cfgInfo {
if info.Secret { if info.Secret {
log.L.Debug(logRunningConfiguration, info.Name, "REDACTED") log.Debug(logRunningConfiguration, info.Name, "REDACTED")
} else { } else {
switch info.Type.String() { switch info.Type.String() {
case "string": case "string":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*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": case "bool":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*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": case "int":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*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 // fatal
case level <= 20: case level <= 20:
L.SLogLevel.Set(LevelFatal) L.SLogLevel.Set(LevelFatal)
L.Info(llu, "level", LevelFatal) Info(llu, "level", LevelFatal)
// error // error
case level > 20 && level <= 40: case level > 20 && level <= 40:
L.SLogLevel.Set(slog.LevelError) L.SLogLevel.Set(slog.LevelError)
L.Info(llu, "level", slog.LevelError) Info(llu, "level", slog.LevelError)
// warning // warning
case level > 40 && level <= 60: case level > 40 && level <= 60:
L.SLogLevel.Set(slog.LevelWarn) L.SLogLevel.Set(slog.LevelWarn)
L.Info(llu, "level", slog.LevelWarn) Info(llu, "level", slog.LevelWarn)
// info // info
case level > 60 && level <= 80: case level > 60 && level <= 80:
L.SLogLevel.Set(slog.LevelInfo) L.SLogLevel.Set(slog.LevelInfo)
L.Info(llu, "level", slog.LevelInfo) Info(llu, "level", slog.LevelInfo)
// debug // debug
case level > 80 && level <= 99: case level > 80 && level <= 99:
L.SLogLevel.Set(slog.LevelDebug) L.SLogLevel.Set(slog.LevelDebug)
L.Info(llu, "level", slog.LevelDebug) Info(llu, "level", slog.LevelDebug)
// trace // trace
case level > 99: case level > 99:
L.SLogLevel.Set(LevelTrace) L.SLogLevel.Set(LevelTrace)
L.Info(llu, "level", LevelTrace) Info(llu, "level", LevelTrace)
} }
// set default logger // set default logger
slog.SetDefault(L.Log) slog.SetDefault(L.Log)
} }
func (log *Log) Fatal(msg string, attrs ...interface{}) { func Fatal(msg string, attrs ...interface{}) {
log.Log.Log( L.Log.Log(
log.Ctx, L.Ctx,
LevelFatal, LevelFatal,
msg, msg,
attrs..., attrs...,
) )
} }
func (log *Log) Error(msg string, attrs ...interface{}) { func Error(msg string, attrs ...interface{}) {
log.Log.Log( L.Log.Log(
log.Ctx, L.Ctx,
slog.LevelError, slog.LevelError,
msg, msg,
attrs..., attrs...,
) )
} }
func (log *Log) Warn(msg string, attrs ...interface{}) { func Warn(msg string, attrs ...interface{}) {
log.Log.Log( L.Log.Log(
log.Ctx, L.Ctx,
slog.LevelWarn, slog.LevelWarn,
msg, msg,
attrs..., attrs...,
) )
} }
func (log *Log) Info(msg string, attrs ...interface{}) { func Info(msg string, attrs ...interface{}) {
log.Log.Log( L.Log.Log(
log.Ctx, L.Ctx,
slog.LevelInfo, slog.LevelInfo,
msg, msg,
attrs..., attrs...,
) )
} }
func (log *Log) Debug(msg string, attrs ...interface{}) { func Debug(msg string, attrs ...interface{}) {
log.Log.Log( L.Log.Log(
log.Ctx, L.Ctx,
slog.LevelDebug, slog.LevelDebug,
msg, msg,
attrs..., attrs...,
) )
} }
func (log *Log) Trace(msg string, attrs ...interface{}) { func Trace(msg string, attrs ...interface{}) {
log.Log.Log( L.Log.Log(
log.Ctx, L.Ctx,
LevelTrace, LevelTrace,
msg, msg,
attrs..., attrs...,

View File

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