Skip to content

Commit

Permalink
go.d.plugin add notice log level (netdata#17112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 committed Mar 5, 2024
1 parent 5f024e4 commit 291004e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/go/collectors/go.d.plugin/logger/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ func newTextHandler() slog.Handler {
return slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: Level.lvl,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && isJournal {
return slog.Attr{}
}
if a.Key == slog.LevelKey {
v := a.Value.Any().(slog.Level)
a.Value = slog.StringValue(strings.ToLower(v.String()))
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
},
Expand All @@ -31,11 +37,18 @@ func newTerminalHandler() slog.Handler {
AddSource: true,
Level: Level.lvl,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
if a.Key == slog.SourceKey && !Level.Enabled(slog.LevelDebug) {
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
},
Expand Down
13 changes: 13 additions & 0 deletions src/go/collectors/go.d.plugin/logger/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import (
"strings"
)

const levelNotice = slog.Level(2)

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 {
Expand All @@ -27,6 +38,8 @@ func (l *level) SetByName(level string) {
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":
Expand Down
2 changes: 2 additions & 0 deletions src/go/collectors/go.d.plugin/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ type Logger struct {

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) }
Expand Down

0 comments on commit 291004e

Please sign in to comment.