summaryrefslogtreecommitdiffstats
path: root/src/go/logger/default.go
blob: c8bfb4d42498518af31e157064394820a93d4bf6 (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
// 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...) }