log

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package log provides a wrapper around the slog package (might change implementation later). It provides an opinionated interface for logging structured data always with context.

Index

Constants

View Source
const (
	FormatCLI     = "cli"
	FormatConsole = "console"
	FormatJSON    = "json"
	FormatLogfmt  = "logfmt"

	ColorForce   = "force"
	ColorDisable = "disable"
	ColorAuto    = "auto"
)

Variables

This section is empty.

Functions

func BindFlags

func BindFlags(flags *pflag.FlagSet, cfg *Config)

BindFlags binds the standard flags to provide logging config at runtime.

func Debug

func Debug(ctx context.Context, msg string, attrs ...any)

Debug logs the message and attributes at default level.

func Error

func Error(ctx context.Context, msg string, err error, attrs ...any)

Error logs the message and error and arguments at error level. If err is nil, it will not be logged.

func Hex7

func Hex7(key string, value []byte) slog.Attr

Hex7 is a convenience function for a hex-encoded log attribute limited to first 7 chars for brevity. Note this is NOT 0x prefixed.

func Info

func Info(ctx context.Context, msg string, attrs ...any)

Info logs the message and attributes at info level.

func Init

func Init(ctx context.Context, cfg Config) (context.Context, error)

Init initializes the global logger with the given config. It also returns a copy of the context with the logger attached, see WithLogger. It returns an error if the config is invalid.

func Warn

func Warn(ctx context.Context, msg string, err error, attrs ...any)

Warn logs the message and error and attributes at warning level. If err is nil, it will not be logged.

func WithCLILogger

func WithCLILogger(ctx context.Context) context.Context

WithCLILogger returns a copy of the context with a cli logger.

func WithCtx

func WithCtx(ctx context.Context, attrs ...any) context.Context

WithCtx returns a copy of the context with which the logging attributes are associated. Usage:

ctx := log.WithCtx(ctx, "height", 1234)
...
log.Info(ctx, "Height processed") // Will contain attribute: height=1234

func WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

WithLogger returns a copy of the context with which the logger is associated replacing the default global logger when logging with this context.

func WithNoopLogger

func WithNoopLogger(ctx context.Context) context.Context

WithNoopLogger returns a copy of the context with a noop logger which discards all logs.

func WithSkip

func WithSkip(ctx context.Context, skip int) context.Context

WithSkip returns a copy of the context with the skip value set. This is used to control the number of stack frames to skip when `source` is calculated.

Types

type Config

type Config struct {
	Level  string
	Color  string
	Format string
}

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default config.

Jump to

Keyboard shortcuts

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