log

package
v0.0.2 Latest Latest
Warning

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

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

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

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

func Err

func Err(err error) slog.Attr

Err returns an Attr that represents an error.

func Errorf

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

func Fatal

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

Fatal for logging fatal errors

func FilePath

func FilePath(filePath string) slog.Attr

FilePath returns an Attr that represents a filePath.

func Infof

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

func Prefix(prefix string) slog.Attr

Prefix returns an Attr that represents a prefix.

func Warnf

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

func WithContextAttrs

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

WithContextAttrs returns a new context with the given attrs.

func WithContextPrefix

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

WithContextPrefix returns a new context with the given prefix.

Types

type ColorHandler

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

func NewHandler

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

func (*ColorHandler) Enabled

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

func (*ColorHandler) Err

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

Err returns the error from the attrs, if any.

func (*ColorHandler) Handle

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

func (*ColorHandler) Prefix

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

Prefix returns the prefix from the attrs, if any.

func (*ColorHandler) WithAttrs

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

func (*ColorHandler) WithGroup

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

type DeferredHandler

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

func (*DeferredHandler) Flush

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

func (*DeferredHandler) Handle

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

func (*DeferredHandler) WithAttrs

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

func (*DeferredHandler) WithGroup

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

type Logger

type Logger = slog.Logger

Logger is an alias of slog.Logger

func New

func New(h slog.Handler) *Logger

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

func WithPrefix

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 Tunnel'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

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