log

package
v0.58.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2024 License: Apache-2.0 Imports: 14 Imported by: 83

Documentation

Index

Constants

View Source
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"
	PrefixVulnerabilityDB  = "vulndb"
	PrefixJavaDB           = "javadb"
)

Variables

View Source
var (
	String   = slog.String
	Int64    = slog.Int64
	Int      = slog.Int
	Bool     = slog.Bool
	Time     = slog.Time
	Duration = slog.Duration
	Group    = slog.Group
	Any      = slog.Any
)
View Source
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
)

Functions

func Debugf added in v0.51.0

func Debugf(format string, args ...any)

func Err added in v0.51.0

func Err(err error) slog.Attr

Err returns an Attr that represents an error.

func Errorf added in v0.51.0

func Errorf(format string, args ...any)

func Fatal

func Fatal(msg string, args ...any)

Fatal for logging fatal errors

func FilePath added in v0.54.0

func FilePath(filePath string) slog.Attr

FilePath returns an Attr that represents a filePath.

func Infof added in v0.51.0

func Infof(format string, args ...any)

func InitLogger

func InitLogger(debug, disable bool)

InitLogger initializes the logger variable and flushes the buffered logs if needed.

func Prefix added in v0.51.0

func Prefix(prefix string) slog.Attr

Prefix returns an Attr that represents a prefix.

func Warnf added in v0.51.0

func Warnf(format string, args ...any)

func WithContextAttrs added in v0.51.0

func WithContextAttrs(ctx context.Context, attrs ...slog.Attr) context.Context

WithContextAttrs returns a new context with the given attrs.

func WithContextPrefix added in v0.51.0

func WithContextPrefix(ctx context.Context, prefix string) context.Context

WithContextPrefix returns a new context with the given prefix.

Types

type ColorHandler added in v0.51.0

type ColorHandler struct {
	// contains filtered or unexported fields
}

func NewHandler added in v0.51.0

func NewHandler(out io.Writer, opts *Options) *ColorHandler

func (*ColorHandler) Enabled added in v0.51.0

func (h *ColorHandler) Enabled(_ context.Context, level slog.Level) bool

func (*ColorHandler) Err added in v0.51.0

func (h *ColorHandler) Err(r slog.Record) error

Err returns the error from the attrs, if any.

func (*ColorHandler) Handle added in v0.51.0

func (h *ColorHandler) Handle(ctx context.Context, r slog.Record) error

func (*ColorHandler) Prefix added in v0.51.0

func (h *ColorHandler) Prefix(ctx context.Context, r slog.Record) string

Prefix returns the prefix from the attrs, if any.

func (*ColorHandler) WithAttrs added in v0.51.0

func (h *ColorHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*ColorHandler) WithGroup added in v0.51.0

func (h *ColorHandler) WithGroup(name string) slog.Handler

type DeferredHandler added in v0.55.0

type DeferredHandler struct {
	// contains filtered or unexported fields
}

DeferredHandler is needed to save logs and print them after calling `PrintLogs()` command. For example, this may be necessary when the logger is not yet initialized, but messages need to be transmitted. In this case, the messages are saved and printed when the logger is initialized.

func (*DeferredHandler) Enabled added in v0.55.0

func (*DeferredHandler) Flush added in v0.55.0

func (d *DeferredHandler) Flush(h slog.Handler)

func (*DeferredHandler) Handle added in v0.55.0

func (d *DeferredHandler) Handle(ctx context.Context, record slog.Record) error

func (*DeferredHandler) WithAttrs added in v0.55.0

func (d *DeferredHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*DeferredHandler) WithGroup added in v0.55.0

func (*DeferredHandler) WithGroup(_ string) slog.Handler

type Logger

type Logger = slog.Logger

Logger is an alias of slog.Logger

func New added in v0.51.0

func New(h slog.Handler) *Logger

New creates a new Logger with the given non-nil Handler.

func WithPrefix added in v0.51.0

func WithPrefix(prefix string) *Logger

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.

type Options added in v0.51.0

type Options struct {
	// Level reports the minimum level to log.
	// Levels with lower levels are discarded.
	// If nil, the Handler uses [slog.LevelInfo].
	Level slog.Leveler
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL