187 lines
3.7 KiB
Go
187 lines
3.7 KiB
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"os"
|
|
"regexp"
|
|
)
|
|
|
|
func New(c Customization) {
|
|
// check configuration values and set defaults for missing items
|
|
setDefaults(&c)
|
|
|
|
slogOptions := &slog.HandlerOptions{
|
|
Level: &L.SLogLevel,
|
|
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
|
if a.Key == slog.TimeKey {
|
|
// set time key
|
|
a.Value = slog.StringValue(c.TimeStamp.Key)
|
|
// set time format
|
|
a.Value = slog.StringValue(a.Value.Time().Format(c.TimeStamp.Format))
|
|
}
|
|
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 c.Format {
|
|
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 setDefaults(c *Customization) error {
|
|
// \/ \/ \/ REQUIRED FIELDS \/ \/ \/ //
|
|
|
|
// format, can be one of "json" or "text"
|
|
switch {
|
|
case len(c.Format) == 0:
|
|
c.Format = "json"
|
|
case c.Format == "json" || c.Format == "text":
|
|
break
|
|
default:
|
|
return errors.New("Invalid format requested.")
|
|
}
|
|
|
|
// set default timestamp identifier
|
|
if len(c.TimeStamp.Key) == 0 {
|
|
c.TimeStamp.Key = "ts"
|
|
}
|
|
|
|
// set default timestamp format
|
|
if len(c.TimeStamp.Format) == 0 {
|
|
c.TimeStamp.Format = "2006-01-02T15:04:05.000-0700"
|
|
}
|
|
|
|
// set default for type of event
|
|
switch {
|
|
case len(c.Type) == 0:
|
|
c.Type = "Business"
|
|
case c.Type == "Business" || c.Type == "Performance" || c.Type == "Security":
|
|
break
|
|
default:
|
|
return errors.New("Invalid type of event, must be one of \"Business\", \"Performance\", or \"Security\".")
|
|
}
|
|
|
|
// set default for application
|
|
if !regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`).MatchString(string(c.Application)) {
|
|
return errors.New("Invalid application id, must be a valid uuid string.")
|
|
}
|
|
|
|
|
|
|
|
// /\ /\ /\ REQUIRED FIELDS /\ /\ /\ //
|
|
|
|
return nil
|
|
}
|
|
|
|
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...,
|
|
)
|
|
}
|