diff options
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/logger/default.go | 30 | ||||
-rw-r--r-- | src/go/collectors/go.d.plugin/logger/handler.go | 90 | ||||
-rw-r--r-- | src/go/collectors/go.d.plugin/logger/level.go | 54 | ||||
-rw-r--r-- | src/go/collectors/go.d.plugin/logger/logger.go | 104 | ||||
-rw-r--r-- | src/go/collectors/go.d.plugin/logger/logger_test.go | 21 |
5 files changed, 299 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/logger/default.go b/src/go/collectors/go.d.plugin/logger/default.go new file mode 100644 index 000000000..c8bfb4d42 --- /dev/null +++ b/src/go/collectors/go.d.plugin/logger/default.go @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package logger + +import ( + "log/slog" + "os" + + "github.com/mattn/go-isatty" +) + +func newDefaultLogger() *Logger { + if isatty.IsTerminal(os.Stderr.Fd()) { + // skip 2 slog pkg calls, 3 this pkg calls + return &Logger{sl: slog.New(withCallDepth(5, newTerminalHandler()))} + } + return &Logger{sl: slog.New(newTextHandler()).With(pluginAttr)} +} + +var defaultLogger = newDefaultLogger() + +func Error(a ...any) { defaultLogger.Error(a...) } +func Warning(a ...any) { defaultLogger.Warning(a...) } +func Info(a ...any) { defaultLogger.Info(a...) } +func Debug(a ...any) { defaultLogger.Debug(a...) } +func Errorf(format string, a ...any) { defaultLogger.Errorf(format, a...) } +func Warningf(format string, a ...any) { defaultLogger.Warningf(format, a...) } +func Infof(format string, a ...any) { defaultLogger.Infof(format, a...) } +func Debugf(format string, a ...any) { defaultLogger.Debugf(format, a...) } +func With(args ...any) *Logger { return defaultLogger.With(args...) } diff --git a/src/go/collectors/go.d.plugin/logger/handler.go b/src/go/collectors/go.d.plugin/logger/handler.go new file mode 100644 index 000000000..40282ead6 --- /dev/null +++ b/src/go/collectors/go.d.plugin/logger/handler.go @@ -0,0 +1,90 @@ +package logger + +import ( + "context" + "log/slog" + "os" + "runtime" + "strings" + + "github.com/lmittmann/tint" +) + +func newTextHandler() slog.Handler { + return slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ + Level: Level.lvl, + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + switch a.Key { + case slog.TimeKey: + if isJournal { + return slog.Attr{} + } + case slog.LevelKey: + lvl := a.Value.Any().(slog.Level) + s, ok := customLevels[lvl] + if !ok { + s = lvl.String() + } + return slog.String(a.Key, strings.ToLower(s)) + } + return a + }, + }) +} + +func newTerminalHandler() slog.Handler { + return tint.NewHandler(os.Stderr, &tint.Options{ + AddSource: true, + Level: Level.lvl, + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + switch a.Key { + case slog.TimeKey: + return slog.Attr{} + case slog.SourceKey: + if !Level.Enabled(slog.LevelDebug) { + return slog.Attr{} + } + case slog.LevelKey: + lvl := a.Value.Any().(slog.Level) + if s, ok := customLevelsTerm[lvl]; ok { + return slog.String(a.Key, s) + } + } + return a + }, + }) +} + +func withCallDepth(depth int, sh slog.Handler) slog.Handler { + if v, ok := sh.(*callDepthHandler); ok { + sh = v.sh + } + return &callDepthHandler{depth: depth, sh: sh} +} + +type callDepthHandler struct { + depth int + sh slog.Handler +} + +func (h *callDepthHandler) Enabled(ctx context.Context, level slog.Level) bool { + return h.sh.Enabled(ctx, level) +} + +func (h *callDepthHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return withCallDepth(h.depth, h.sh.WithAttrs(attrs)) +} + +func (h *callDepthHandler) WithGroup(name string) slog.Handler { + return withCallDepth(h.depth, h.sh.WithGroup(name)) +} + +func (h *callDepthHandler) Handle(ctx context.Context, r slog.Record) error { + // https://pkg.go.dev/log/slog#example-package-Wrapping + var pcs [1]uintptr + // skip Callers and this function + runtime.Callers(h.depth+2, pcs[:]) + r.PC = pcs[0] + + return h.sh.Handle(ctx, r) +} diff --git a/src/go/collectors/go.d.plugin/logger/level.go b/src/go/collectors/go.d.plugin/logger/level.go new file mode 100644 index 000000000..97dccb205 --- /dev/null +++ b/src/go/collectors/go.d.plugin/logger/level.go @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package logger + +import ( + "log/slog" + "strings" +) + +const ( + levelNotice = slog.Level(2) + levelDisable = slog.Level(99) +) + +var ( + customLevels = map[slog.Leveler]string{ + levelNotice: "NOTICE", + } + customLevelsTerm = map[slog.Leveler]string{ + levelNotice: "\u001B[34m" + "NTC" + "\u001B[0m", + } +) + +var Level = &level{lvl: &slog.LevelVar{}} + +type level struct { + lvl *slog.LevelVar +} + +func (l *level) Enabled(level slog.Level) bool { + return level >= l.lvl.Level() +} + +func (l *level) Set(level slog.Level) { + l.lvl.Set(level) +} + +func (l *level) SetByName(level string) { + // https://github.com/netdata/netdata/tree/master/src/libnetdata/log#log-levels + switch strings.ToLower(level) { + case "err", "error": + l.lvl.Set(slog.LevelError) + case "warn", "warning": + l.lvl.Set(slog.LevelWarn) + case "notice": + l.lvl.Set(levelNotice) + case "info": + l.lvl.Set(slog.LevelInfo) + case "debug": + l.lvl.Set(slog.LevelDebug) + case "emergency", "alert", "critical": + l.lvl.Set(levelDisable) + } +} diff --git a/src/go/collectors/go.d.plugin/logger/logger.go b/src/go/collectors/go.d.plugin/logger/logger.go new file mode 100644 index 000000000..bccf3f0d6 --- /dev/null +++ b/src/go/collectors/go.d.plugin/logger/logger.go @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package logger + +import ( + "context" + "fmt" + "log/slog" + "os" + "strconv" + "strings" + "sync/atomic" + "syscall" + + "github.com/netdata/netdata/go/go.d.plugin/agent/executable" + + "github.com/mattn/go-isatty" +) + +var isTerm = isatty.IsTerminal(os.Stderr.Fd()) + +var isJournal = isStderrConnectedToJournal() + +var pluginAttr = slog.String("plugin", executable.Name) + +func New() *Logger { + if isTerm { + // skip 2 slog pkg calls, 2 this pkg calls + return &Logger{sl: slog.New(withCallDepth(4, newTerminalHandler()))} + } + return &Logger{sl: slog.New(newTextHandler()).With(pluginAttr)} +} + +type Logger struct { + muted atomic.Bool + sl *slog.Logger +} + +func (l *Logger) Error(a ...any) { l.log(slog.LevelError, fmt.Sprint(a...)) } +func (l *Logger) Warning(a ...any) { l.log(slog.LevelWarn, fmt.Sprint(a...)) } +func (l *Logger) Notice(a ...any) { l.log(levelNotice, fmt.Sprint(a...)) } +func (l *Logger) Info(a ...any) { l.log(slog.LevelInfo, fmt.Sprint(a...)) } +func (l *Logger) Debug(a ...any) { l.log(slog.LevelDebug, fmt.Sprint(a...)) } +func (l *Logger) Errorf(format string, a ...any) { l.log(slog.LevelError, fmt.Sprintf(format, a...)) } +func (l *Logger) Warningf(format string, a ...any) { l.log(slog.LevelWarn, fmt.Sprintf(format, a...)) } +func (l *Logger) Noticef(format string, a ...any) { l.log(levelNotice, fmt.Sprintf(format, a...)) } +func (l *Logger) Infof(format string, a ...any) { l.log(slog.LevelInfo, fmt.Sprintf(format, a...)) } +func (l *Logger) Debugf(format string, a ...any) { l.log(slog.LevelDebug, fmt.Sprintf(format, a...)) } +func (l *Logger) Mute() { l.mute(true) } +func (l *Logger) Unmute() { l.mute(false) } + +func (l *Logger) With(args ...any) *Logger { + if l.isNil() { + return &Logger{sl: New().sl.With(args...)} + } + + ll := &Logger{sl: l.sl.With(args...)} + ll.muted.Store(l.muted.Load()) + + return ll +} + +func (l *Logger) log(level slog.Level, msg string) { + if l.isNil() { + nilLogger.sl.Log(context.Background(), level, msg) + return + } + + if !l.muted.Load() { + l.sl.Log(context.Background(), level, msg) + } +} + +func (l *Logger) mute(v bool) { + if l.isNil() || isTerm && Level.Enabled(slog.LevelDebug) { + return + } + l.muted.Store(v) +} + +func (l *Logger) isNil() bool { return l == nil || l.sl == nil } + +var nilLogger = New() + +func isStderrConnectedToJournal() bool { + stream := os.Getenv("JOURNAL_STREAM") + if stream == "" { + return false + } + + idx := strings.IndexByte(stream, ':') + if idx <= 0 { + return false + } + + dev, ino := stream[:idx], stream[idx+1:] + + var stat syscall.Stat_t + if err := syscall.Fstat(int(os.Stderr.Fd()), &stat); err != nil { + return false + } + + return dev == strconv.Itoa(int(stat.Dev)) && ino == strconv.FormatUint(stat.Ino, 10) +} diff --git a/src/go/collectors/go.d.plugin/logger/logger_test.go b/src/go/collectors/go.d.plugin/logger/logger_test.go new file mode 100644 index 000000000..df7049d0a --- /dev/null +++ b/src/go/collectors/go.d.plugin/logger/logger_test.go @@ -0,0 +1,21 @@ +package logger + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNew(t *testing.T) { + tests := map[string]*Logger{ + "default logger": New(), + "nil logger": nil, + } + + for name, logger := range tests { + t.Run(name, func(t *testing.T) { + f := func() { logger.Infof("test %s", "test") } + assert.NotPanics(t, f) + }) + } +} |