summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/logger/logger.go
blob: bccf3f0d64b1865921eee3385fd3e8a056380b7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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)
}