Net 262 - structured logging (#2366)

* structured logging infra

* structured logging mq handlers
This commit is contained in:
Matthew R Kasun
2023-06-06 13:47:16 -04:00
committed by GitHub
parent c6b1ec494c
commit f94cda11f6
4 changed files with 73 additions and 54 deletions

21
main.go
View File

@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"sync"
"syscall"
@@ -26,6 +27,7 @@ import (
"github.com/gravitl/netmaker/servercfg"
"github.com/gravitl/netmaker/serverctl"
stunserver "github.com/gravitl/netmaker/stun-server"
"golang.org/x/exp/slog"
)
var version = "v0.20.2"
@@ -179,6 +181,25 @@ func runMessageQueue(wg *sync.WaitGroup, ctx context.Context) {
func setVerbosity() {
verbose := int(servercfg.GetVerbosity())
logger.Verbosity = verbose
logLevel := &slog.LevelVar{}
replace := func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
a.Value = slog.StringValue(filepath.Base(a.Value.String()))
}
return a
}
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{AddSource: true, ReplaceAttr: replace, Level: logLevel}))
slog.SetDefault(logger)
switch verbose {
case 4:
logLevel.Set(slog.LevelDebug)
case 3:
logLevel.Set(slog.LevelInfo)
case 2:
logLevel.Set(slog.LevelWarn)
default:
logLevel.Set(slog.LevelError)
}
}
func setGarbageCollection() {