Documentation
¶
Index ¶
- Variables
- func Context(parent context.Context, l *slog.Logger) context.Context
- func Discard(ctx context.Context) context.Context
- func Fatal(v ...any)
- func Fatalf(format string, v ...any)
- func Fatalln(v ...any)
- func From(ctx context.Context) *slog.Logger
- func New(opts ...Option) *slog.Logger
- func Panic(v ...any)
- func Panicf(format string, v ...any)
- func Panicln(v ...any)
- func Print(v ...any)
- func Printf(format string, v ...any)
- func Println(v ...any)
- func SetDefault()
- type DiscardLogger
- type Format
- type Option
- type Options
Constants ¶
This section is empty.
Variables ¶
var DefaultLevel = new(slog.LevelVar)
DefaultLevel is a level variable used to change the logging level for the default logger.
var Key = context.Key[*slog.Logger]()
Key is a context.Key for storing a *slog.Logger in a context.Context.
Functions ¶
func Context ¶
Context returns a context.Context deferred from the provided parent context.Context, storing a *slog.Logger.
func Discard ¶
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 ¶
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 ¶
From returns the *slog.Logger stored in the provided context.Context. If not value is stored, nil is returned.
func New ¶
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 ¶
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 ¶
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 ¶
DiscardLogger is an implementation of slog.Handler that discards all messages.
type Format ¶
type Format int
func (Format) MarshalText ¶
MarshalText implements encoding.TextMarshaler by calling Format.String.
func (*Format) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It accepts any string produced by Format.MarshalText, ignoring case.
type Option ¶
type Option func(*Options)
func WithFormat ¶
WithFormat is an option setting the logger format.
func WithOptions ¶
WithOptions allows passing Options to completely override the existing options.
func WithOutput ¶
WithOutput is an option setting the logger output.
func WithRemoveAttrs ¶
WithRemoveAttrs is an option removing any slog.Attr that match the provided keys.
func WithSource ¶
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 ¶
NewOptions constructs an Options with the provided options. Options not explicitly set will be initialized with defaults.
func (Options) New ¶
New constructs a new slog.Logger with the values of Options.
func (Options) NewLogger ¶
NewLogger constructs a new *slog.Logger from the current values of Options.
func (Options) SetDefault ¶
func (o Options) SetDefault()