Add package slogd

Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
Jan Tytgat
2025-04-15 09:36:56 +02:00
parent 41bf5b86ea
commit 0efc6f033b
5 changed files with 248 additions and 0 deletions

56
pkg/slogd/level.go Normal file
View File

@ -0,0 +1,56 @@
package slogd
import (
"log/slog"
"strings"
)
const (
LevelTrace = slog.Level(-8)
LevelDebug = slog.LevelDebug
LevelInfo = slog.LevelInfo
LevelNotice = slog.Level(2)
LevelWarn = slog.LevelWarn
LevelError = slog.LevelError
LevelFatal = slog.Level(12)
LevelDefault = LevelInfo
)
var levelNames = map[slog.Leveler]string{
LevelTrace: "TRACE",
LevelDebug: "DEBUG",
LevelInfo: "INFO",
LevelNotice: "NOTICE",
LevelWarn: "WARN",
LevelError: "ERROR",
LevelFatal: "FATAL",
}
func ReplaceAttrs(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
a.Value = slog.StringValue(LevelName(a.Value.Any().(slog.Level)))
}
return a
}
func Level(l string) slog.Level {
mux.Lock()
defer mux.Unlock()
for k, v := range levelNames {
if strings.ToUpper(l) == v {
return k.Level()
}
}
return LevelDefault
}
func LevelName(l slog.Level) string {
mux.Lock()
defer mux.Unlock()
for k, v := range levelNames {
if k == l {
return v
}
}
return levelNames[LevelDefault]
}