summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/logger/default.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/go/collectors/go.d.plugin/logger/default.go30
1 files changed, 30 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...) }