-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
logger.go
100 lines (84 loc) · 2.8 KB
/
logger.go
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
package log
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"strings"
"github.com/samber/lo"
)
const (
LevelDebug = slog.LevelDebug
LevelInfo = slog.LevelInfo
LevelWarn = slog.LevelWarn
LevelError = slog.LevelError
LevelFatal = slog.Level(12)
PrefixContainerImage = "image"
PrefixPackage = "pkg"
PrefixVulnerability = "vuln"
PrefixMisconfiguration = "misconfig"
PrefixSecret = "secret"
PrefixLicense = "license"
)
// Logger is an alias of slog.Logger
type Logger = slog.Logger
// New creates a new Logger with the given non-nil Handler.
func New(h slog.Handler) *Logger {
return slog.New(h)
}
// InitLogger initialize the logger variable
func InitLogger(debug, disable bool) {
level := lo.Ternary(debug, slog.LevelDebug, slog.LevelInfo)
out := lo.Ternary(disable, io.Discard, io.Writer(os.Stderr))
slog.SetDefault(New(NewHandler(out, &Options{Level: level})))
}
var (
// With calls [Logger.With] on the default logger.
With = slog.With
SetDefault = slog.SetDefault
Debug = slog.Debug
DebugContext = slog.DebugContext
Info = slog.Info
InfoContext = slog.InfoContext
Warn = slog.Warn
WarnContext = slog.WarnContext
Error = slog.Error
ErrorContext = slog.ErrorContext
)
// WithPrefix calls [Logger.With] with the prefix on the default logger.
//
// Note: If WithPrefix is called within init() or during global variable
// initialization, it will use the default logger of log/slog package
// before Trivy's logger is set up. In such cases, it's recommended to pass the prefix
// via WithContextPrefix to ensure the correct logger is used.
func WithPrefix(prefix string) *Logger {
return slog.Default().With(Prefix(prefix))
}
func Debugf(format string, args ...any) { slog.Default().Debug(fmt.Sprintf(format, args...)) }
func Infof(format string, args ...any) { slog.Default().Info(fmt.Sprintf(format, args...)) }
func Warnf(format string, args ...any) { slog.Default().Warn(fmt.Sprintf(format, args...)) }
func Errorf(format string, args ...any) { slog.Default().Error(fmt.Sprintf(format, args...)) }
// Fatal for logging fatal errors
func Fatal(msg string, args ...any) {
// Fatal errors should be logged to stderr even if the logger is disabled.
if h, ok := slog.Default().Handler().(*ColorHandler); ok {
h.out = os.Stderr
} else {
slog.SetDefault(New(NewHandler(os.Stderr, &Options{})))
}
slog.Default().Log(context.Background(), LevelFatal, msg, args...)
os.Exit(1)
}
// WriteLogger is a wrapper around Logger to implement io.Writer
type WriteLogger struct {
logger *Logger
}
// NewWriteLogger creates a new WriteLogger
func NewWriteLogger(logger *Logger) *WriteLogger {
return &WriteLogger{logger: logger}
}
func (l *WriteLogger) Write(p []byte) (n int, err error) {
l.logger.Debug(strings.TrimSpace(string(p)))
return len(p), nil
}