log

package
v0.0.0-...-0718798 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLevel = new(slog.LevelVar)

DefaultLevel is a level variable used to change the logging level for the default logger.

Key is a context.Key for storing a *slog.Logger in a context.Context.

Functions

func Context

func Context(parent context.Context, l *slog.Logger) context.Context

Context returns a context.Context deferred from the provided parent context.Context, storing a *slog.Logger.

func Discard

func Discard(ctx context.Context) context.Context

Discard stores a slog.Logger in the provided context that discards all log messages.

func Fatal

func Fatal(v ...any)

Fatal is similar to log.Fatal, but uses the default slog.Logger. It is meant to be used at the end of an error chain only.

If GO_BACKTRACE=1 environment variable is set, any arguments containing a stack trace will be printed directly to stderr. Only the first argument containing a stack trace will be printed.

func Fatalf

func Fatalf(format string, v ...any)

Fatalf is similar to log.Fatalf, but uses the default slog.Logger. It is meant to be used at the end of an error chain only.

If GO_BACKTRACE=1 environment variable is set, any arguments containing a stack trace will be printed directly to stderr. Only the first argument containing a stack trace will be printed.

func Fatalln

func Fatalln(v ...any)

Fatalln is similar to log.Fatalln, but uses the default slog.Logger. It is meant to be used at the end of an error chain only.

If GO_BACKTRACE=1 environment variable is set, any arguments containing a stack trace will be printed directly to stderr. Only the first argument containing a stack trace will be printed.

func From

func From(ctx context.Context) *slog.Logger

From returns the *slog.Logger stored in the provided context.Context. If not value is stored, nil is returned.

func New

func New(opts ...Option) *slog.Logger

New constructs a new *slog.Logger with the provided options.

func Panic

func Panic(v ...any)

Panic is equivalent to Print followed by a call to panic().

This is an alias of https://pkg.go.dev/log#Panic.

func Panicf

func Panicf(format string, v ...any)

Panicf is equivalent to Printf followed by a call to panic().

This is an alias of https://pkg.go.dev/log#Panicf.

func Panicln

func Panicln(v ...any)

Panicln is equivalent to Println followed by a call to panic().

This is an alias of https://pkg.go.dev/log#Panicln.

func Print

func Print(v ...any)

Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.

This is an alias of https://pkg.go.dev/log#Print.

func Printf

func Printf(format string, v ...any)

Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

This is an alias of https://pkg.go.dev/log#Printf.

func Println

func Println(v ...any)

Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

This is an alias of https://pkg.go.dev/log#Println.

func SetDefault

func SetDefault()

SetDefault parses configuration from environment variables and constructs a new default slog.Logger. This should be called in the application main package so that any packages can easily access a configured slog.Logger.

Types

type DiscardLogger

type DiscardLogger struct {
	slog.Handler
}

DiscardLogger is an implementation of slog.Handler that discards all messages.

func (*DiscardLogger) Enabled

func (h *DiscardLogger) Enabled(ctx context.Context, lvl slog.Level) bool

type Format

type Format int
const (
	// TextFormat sets log output to logfmt.
	TextFormat Format = iota
	// JSONFormat sets log output format to JSON.
	JSONFormat
)

func (Format) MarshalText

func (f Format) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler by calling Format.String.

func (Format) String

func (f Format) String() string

func (*Format) UnmarshalText

func (f *Format) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It accepts any string produced by Format.MarshalText, ignoring case.

type Option

type Option func(*Options)

func WithFormat

func WithFormat(f Format) Option

WithFormat is an option setting the logger format.

func WithLevel

func WithLevel(lvl slog.Leveler) Option

WithLevel is an option for specifying the level of a logger.

func WithOptions

func WithOptions(opts Options) Option

WithOptions allows passing Options to completely override the existing options.

func WithOutput

func WithOutput(w io.Writer) Option

WithOutput is an option setting the logger output.

func WithRemoveAttrs

func WithRemoveAttrs(attrs ...string) Option

WithRemoveAttrs is an option removing any slog.Attr that match the provided keys.

func WithSource

func WithSource(v bool) Option

WithSource is an option specifying if the source attribute is added to records.

type Options

type Options struct {
	Level       *slog.LevelVar `env:"LOG_LEVEL" default:"INFO"`
	Format      Format         `env:"LOG_FORMAT" default:"text"`
	AddSource   bool           `env:"LOG_ADD_SOURCE" default:"true"`
	RemoveAttrs []string       `env:"LOG_REMOVE_ATTRS"`
	// contains filtered or unexported fields
}

Options specifies the configuration for a logger from environment variables.

func NewOptions

func NewOptions(opts ...Option) *Options

NewOptions constructs an Options with the provided options. Options not explicitly set will be initialized with defaults.

func (Options) LogValue

func (o Options) LogValue() slog.Value

func (Options) New

func (o Options) New() slog.Handler

New constructs a new slog.Logger with the values of Options.

func (Options) NewLogger

func (o Options) NewLogger() *slog.Logger

NewLogger constructs a new *slog.Logger from the current values of Options.

func (Options) SetDefault

func (o Options) SetDefault()

func (*Options) SetOutput

func (o *Options) SetOutput(w io.Writer)

SetOutput sets the output for any derived loggers.

Jump to

Keyboard shortcuts

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