Documentation
¶
Overview ¶
Example ¶
package main
import (
"errors"
"log/slog"
"os"
"time"
"github.com/networkteam/slogutils"
)
func main() {
slog.SetDefault(slog.New(slogutils.NewCLIHandler(os.Stderr, &slogutils.CLIHandlerOptions{
Level: slog.LevelDebug,
})))
slog.Info("Starting server", "addr", ":8080", "env", "production")
slog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
slog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
slog.Error("DB connection lost", slogutils.Err(errors.New("connection reset")), "db", "myapp")
}
Index ¶
Examples ¶
Constants ¶
View Source
const (
// ErrorKey is the key for an error attribute.
ErrorKey = "err"
)
Keys for special attributes.
View Source
const ( // LevelTrace is an extra level for tracing that is lower than debug. LevelTrace slog.Level = -8 )
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext returns a logger instance from the context or the default logger.
Types ¶
type CLIHandler ¶
type CLIHandler struct {
// contains filtered or unexported fields
}
func NewCLIHandler ¶
func NewCLIHandler(w io.Writer, opts *CLIHandlerOptions) *CLIHandler
type CLIHandlerOptions ¶
type CLIHandlerOptions struct {
// Level reports the minimum record level that will be logged.
// The handler discards records with lower levels.
// If Level is nil, the handler assumes LevelInfo.
// The handler calls Level.Level for each record processed;
// to adjust the minimum level dynamically, use a LevelVar.
Level slog.Leveler
// Prefix options for setting a custom padding and level prefixes.
Prefix *PrefixOptions
// LevelColors can set a custom map of level colors.
// It must be complete, i.e. contain all levels.
LevelColors map[slog.Level]*color.Color
// MessagePadding is the number of spaces to pad the message with.
// A default of 25 is used if this is 0.
// Setting it to a negative value disables padding.
MessagePadding int
// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
// See https://pkg.go.dev/log/slog#HandlerOptions for details.
ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr
}
CLIHandlerOptions are options for a CLIHandler. A zero CLIHandlerOptions consists entirely of default values.
Click to show internal directories.
Click to hide internal directories.