@cryptotaxi247 / netdata-1 / commits / 291004e39

go.d.plugin add notice log level (#17112)

Ilya Mashchenko committed Mar 5, 2024 at 21:04 UTC 291004e39ba61d34f3c89f2ef30143e880a0ee78
3 files changed +38 -10
src/go/collectors/go.d.plugin/logger/handler.go
+23 -10
@@ -14,12 +14,18 @@ func newTextHandler() slog.Handler {
14 return slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
15 Level: Level.lvl,
16 ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
17 - if a.Key == slog.TimeKey && isJournal {
18 - return slog.Attr{}
19 - }
20 - if a.Key == slog.LevelKey {
21 - v := a.Value.Any().(slog.Level)
22 - a.Value = slog.StringValue(strings.ToLower(v.String()))
17 + switch a.Key {
18 + case slog.TimeKey:
19 + if isJournal {
20 + return slog.Attr{}
21 + }
22 + case slog.LevelKey:
23 + lvl := a.Value.Any().(slog.Level)
24 + s, ok := customLevels[lvl]
25 + if !ok {
26 + s = lvl.String()
27 + }
28 + return slog.String(a.Key, strings.ToLower(s))
29 }
30 return a
31 },
@@ -31,11 +37,18 @@ func newTerminalHandler() slog.Handler {
37 AddSource: true,
38 Level: Level.lvl,
39 ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
34 - if a.Key == slog.TimeKey {
35 - return slog.Attr{}
36 - }
37 - if a.Key == slog.SourceKey && !Level.Enabled(slog.LevelDebug) {
40 + switch a.Key {
41 + case slog.TimeKey:
42 return slog.Attr{}
43 + case slog.SourceKey:
44 + if !Level.Enabled(slog.LevelDebug) {
45 + return slog.Attr{}
46 + }
47 + case slog.LevelKey:
48 + lvl := a.Value.Any().(slog.Level)
49 + if s, ok := customLevelsTerm[lvl]; ok {
50 + return slog.String(a.Key, s)
51 + }
52 }
53 return a
54 },
src/go/collectors/go.d.plugin/logger/level.go
+13
@@ -7,6 +7,17 @@ import (
7 "strings"
8 )
9
10 +const levelNotice = slog.Level(2)
11 +
12 +var (
13 + customLevels = map[slog.Leveler]string{
14 + levelNotice: "NOTICE",
15 + }
16 + customLevelsTerm = map[slog.Leveler]string{
17 + levelNotice: "\u001B[34m" + "NTC" + "\u001B[0m",
18 + }
19 +)
20 +
21 var Level = &level{lvl: &slog.LevelVar{}}
22
23 type level struct {
@@ -27,6 +38,8 @@ func (l *level) SetByName(level string) {
38 l.lvl.Set(slog.LevelError)
39 case "warn", "warning":
40 l.lvl.Set(slog.LevelWarn)
41 + case "notice":
42 + l.lvl.Set(levelNotice)
43 case "info":
44 l.lvl.Set(slog.LevelInfo)
45 case "debug":
src/go/collectors/go.d.plugin/logger/logger.go
+2
@@ -38,10 +38,12 @@ type Logger struct {
38
39 func (l *Logger) Error(a ...any) { l.log(slog.LevelError, fmt.Sprint(a...)) }
40 func (l *Logger) Warning(a ...any) { l.log(slog.LevelWarn, fmt.Sprint(a...)) }
41 +func (l *Logger) Notice(a ...any) { l.log(levelNotice, fmt.Sprint(a...)) }
42 func (l *Logger) Info(a ...any) { l.log(slog.LevelInfo, fmt.Sprint(a...)) }
43 func (l *Logger) Debug(a ...any) { l.log(slog.LevelDebug, fmt.Sprint(a...)) }
44 func (l *Logger) Errorf(format string, a ...any) { l.log(slog.LevelError, fmt.Sprintf(format, a...)) }
45 func (l *Logger) Warningf(format string, a ...any) { l.log(slog.LevelWarn, fmt.Sprintf(format, a...)) }
46 +func (l *Logger) Noticef(format string, a ...any) { l.log(levelNotice, fmt.Sprintf(format, a...)) }
47 func (l *Logger) Infof(format string, a ...any) { l.log(slog.LevelInfo, fmt.Sprintf(format, a...)) }
48 func (l *Logger) Debugf(format string, a ...any) { l.log(slog.LevelDebug, fmt.Sprintf(format, a...)) }
49 func (l *Logger) Mute() { l.mute(true) }