Compare commits

...

8 Commits

7 changed files with 290 additions and 77 deletions

View File

@@ -14,7 +14,7 @@
"editor.insertSpaces": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "always"
}
},

View File

@@ -130,7 +130,7 @@ func getAlt(ftype reflect.StructField) string {
func getDefault(ftype reflect.StructField) interface{} {
if ftype.Tag.Get("default") != "" {
v, err := typeConversion(ftype.Type.String(), ftype.Tag.Get("Default"))
v, err := typeConversion(ftype.Type.String(), ftype.Tag.Get("default"))
if err != nil {
return nil
}

View File

@@ -1,32 +1,36 @@
package config
import (
"log"
l "log"
"os"
"time"
"example.com/golang-base/internal/log"
)
func Init() Config {
cfg := New()
// 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
setLogLevel(&cfg)
log.Init("text")
log.SetNumericLevel(cfg.LogLevel)
// set timezone & time format
cfg.TZUTC, _ = time.LoadLocation("UTC")
cfg.TZLocal, err = time.LoadLocation(cfg.TimeZoneLocal)
if err != nil {
cfg.Log.Error("Unable to parse timezone string", "error", err)
log.Error("Unable to parse timezone string", "error", err)
os.Exit(1)
}

View File

@@ -1,13 +1,19 @@
package config
import (
"log/slog"
"os"
"reflect"
"strconv"
"time"
"example.com/golang-base/internal/log"
)
// Config uses struct tags to configure the application.
// (default) Default value to be used if unset or not defined.
// (ignored) Don't process the current tag.
// (info) String to be presented to the user on -help use.
// (secret) If set to true, hide the value from being output on start-up.
// (env) environment variable to be used if not set on command line.
type Config struct {
// time configuration
TimeFormat string `default:"2006-01-02 15:04:05" env:"time_format"`
@@ -16,9 +22,7 @@ type Config struct {
TZUTC *time.Location `ignored:"true"`
// logging
LogLevel int `default:"50" env:"log_level"`
Log *slog.Logger `ignored:"true"`
SLogLevel *slog.LevelVar `ignored:"true"`
LogLevel int `default:"50" env:"log_level"`
// webserver
WebServerPort int `default:"8080" env:"webserver_port"`
@@ -30,57 +34,26 @@ type Config struct {
// New initializes the config variable for use with a prepared set of defaults.
func New() Config {
cfg := Config{
SLogLevel: new(slog.LevelVar),
}
cfg.Log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: cfg.SLogLevel,
}))
return cfg
}
func setLogLevel(cfg *Config) {
switch {
// error
case cfg.LogLevel <= 20:
cfg.SLogLevel.Set(slog.LevelError)
cfg.Log.Info("Log level updated", "level", slog.LevelError)
// warning
case cfg.LogLevel > 20 && cfg.LogLevel <= 40:
cfg.SLogLevel.Set(slog.LevelWarn)
cfg.Log.Info("Log level updated", "level", slog.LevelWarn)
// info
case cfg.LogLevel > 40 && cfg.LogLevel <= 60:
cfg.SLogLevel.Set(slog.LevelInfo)
cfg.Log.Info("Log level updated", "level", slog.LevelInfo)
// debug
case cfg.LogLevel > 60:
cfg.SLogLevel.Set(slog.LevelDebug)
cfg.Log.Info("Log level updated", "level", slog.LevelDebug)
}
// set default logger
slog.SetDefault(cfg.Log)
return Config{}
}
func printRunningConfig(cfg *Config, cfgInfo []structInfo) {
var logRunningConfiguration = "Running Configuration"
var logRunningConfiguration string = "Running Configuration"
for _, info := range cfgInfo {
if info.Secret {
cfg.Log.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)
cfg.Log.Debug("Running Configuration", info.Alt, *p)
log.Debug(logRunningConfiguration, info.Alt, *p)
case "bool":
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*bool)
cfg.Log.Debug("Running Configuration", 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)
cfg.Log.Debug("Running Configuration", info.Alt, strconv.FormatInt(int64(*p), 10))
log.Debug(logRunningConfiguration, info.Alt, strconv.FormatInt(int64(*p), 10))
}
}
}

View File

@@ -6,6 +6,7 @@ import (
"reflect"
"testing"
"example.com/golang-base/internal/log"
"github.com/stretchr/testify/assert"
)
@@ -15,44 +16,25 @@ func slogToBuffer() (*bytes.Buffer, *slog.Logger) {
slog.NewTextHandler(
buf,
&slog.HandlerOptions{
Level: slog.LevelDebug,
Level: log.LevelTrace,
},
),
)
}
func TestNew(t *testing.T) {
c := New()
assert.Equal(t, "config.Config", reflect.TypeOf(c).String())
}
func TestSetLogLevel(t *testing.T) {
c := New()
for _, i := range []int{0, 21, 41, 61} {
c.LogLevel = i
setLogLevel(&c)
switch i {
case 0:
assert.Equal(t, slog.LevelError, c.SLogLevel.Level())
case 21:
assert.Equal(t, slog.LevelWarn, c.SLogLevel.Level())
case 41:
assert.Equal(t, slog.LevelInfo, c.SLogLevel.Level())
case 61:
assert.Equal(t, slog.LevelDebug, c.SLogLevel.Level())
}
}
}
func TestPrintRunningConfig(t *testing.T) {
buf, log := slogToBuffer()
buf, l := slogToBuffer()
log.L.Log = l
c := New()
c.Log = log
cfgInfo, err := getStructInfo(&c)
assert.NoError(t, err)
printRunningConfig(&c, cfgInfo)
assert.Contains(t, buf.String(), "Running Configuration")
}
func TestNew(t *testing.T) {
c := New()
assert.Equal(t, "config.Config", reflect.TypeOf(c).String())
}

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

@@ -0,0 +1,157 @@
package log
import (
"context"
"log/slog"
"os"
"time"
)
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(writer string) {
slogOptions := &slog.HandlerOptions{
Level: &L.SLogLevel,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
a.Value = slog.StringValue(a.Value.Time().Format(time.DateTime))
// To disable the timestamp all together, return an empty value.
// return 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
},
}
// Initialize SLog and translate new logging levels
switch writer {
case "json":
L.Log = slog.New(slog.NewJSONHandler(os.Stdout, slogOptions))
default:
L.Log = slog.New(slog.NewTextHandler(os.Stdout, slogOptions))
}
// create context
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)
Info(llu, "level", LevelFatal)
// error
case level > 20 && level <= 40:
L.SLogLevel.Set(slog.LevelError)
Info(llu, "level", slog.LevelError)
// warning
case level > 40 && level <= 60:
L.SLogLevel.Set(slog.LevelWarn)
Info(llu, "level", slog.LevelWarn)
// info
case level > 60 && level <= 80:
L.SLogLevel.Set(slog.LevelInfo)
Info(llu, "level", slog.LevelInfo)
// debug
case level > 80 && level <= 99:
L.SLogLevel.Set(slog.LevelDebug)
Info(llu, "level", slog.LevelDebug)
// trace
case level > 99:
L.SLogLevel.Set(LevelTrace)
Info(llu, "level", LevelTrace)
}
// set default logger
slog.SetDefault(L.Log)
}
func Fatal(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
LevelFatal,
msg,
attrs...,
)
}
func Error(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelError,
msg,
attrs...,
)
}
func Warn(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelWarn,
msg,
attrs...,
)
}
func Info(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelInfo,
msg,
attrs...,
)
}
func Debug(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
slog.LevelDebug,
msg,
attrs...,
)
}
func Trace(msg string, attrs ...interface{}) {
L.Log.Log(
L.Ctx,
LevelTrace,
msg,
attrs...,
)
}

View File

@@ -0,0 +1,97 @@
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) {
Init("text")
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
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
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
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
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
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
Trace("TEST Message")
assert.Contains(t, buf.String(), "TEST Message")
assert.Contains(t, buf.String(), "level=DEBUG-4")
}