slog

package
v5.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Reset     = "\033[0m"
	Red       = "\033[31m"
	Yellow    = "\033[33m"
	Blue      = "\033[34m"
	Gray      = "\033[90m"
	WhiteBold = "\033[37;1m"
	GrayBold  = "\033[90;1m"
	BGYellow  = "\033[43m"
	BGRed     = "\033[41m"
	BGCyan    = "\033[46m"
	BGGray    = "\033[100m"
)

Colors and formats

Variables

View Source
var (
	// Indent the string used to indent attribute groups in the dev mode handler.
	Indent = "  "
)

Functions

func NewHandler

func NewHandler(devMode bool, w io.Writer) slog.Handler

NewHandler creates a new `slog.Handler` with default options. If `devMode` is true, a `*DevModeHandler` is returned, else a `*slog.JSONHandler`.

func StructValue

func StructValue(v any) slog.Value

StructValue recursively convert a structure, structure pointer or map to a `slog.GroupValue`. If the given value implements `slog.LogValuer`, this value is returned instead. Returns AnyValue if the type is not supported.

Types

type DevModeHandler

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

DevModeHandler is a `slog.Handler` that writes Records to an io.Writer. The records are formatted to be easily readable by humans. This handler is meant for development use only as it doesn't provide optimal performance and its output is not machine-readable.

func NewDevModeHandler

func NewDevModeHandler(w io.Writer, opts *DevModeHandlerOptions) *DevModeHandler

NewDevModeHandler creates a new `DevModeHandler` that writes to w, using the given options. If `opts` is `nil`, the default options are used.

func (*DevModeHandler) Enabled

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

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*DevModeHandler) Handle

func (h *DevModeHandler) Handle(_ context.Context, r slog.Record) error

Handle formats its argument `Record` in an output easily readable by humans. The output contains multiple lines:

  • The first one contains the log level, the time and the source
  • The second one contains the message
  • The next lines contain the attributes and groups, if any

Each call to `Handle` results in a single serialized call to `io.Writer.Write()`.

func (*DevModeHandler) WithAttrs

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

WithAttrs returns a new `DevModeHandler` whose attributes consists of h's attributes followed by attrs.

func (*DevModeHandler) WithGroup

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

WithGroup returns a new `DevModeHandler` whose attributes are wrapped into a named group. All the handler's attributes will be printed indented into the added group.

type DevModeHandlerOptions

type DevModeHandlerOptions 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 `slog.LevelVar`.
	Level slog.Leveler
}

DevModeHandlerOptions options for the dev mode handler.

type Logger

type Logger struct {
	*slog.Logger
}

Logger an extension of standard `*slog.Logger` overriding the `Error()` and `ErrorCtx()` functions so they take an error as parameter and handle `*errors.Error` gracefully.

func New

func New(h slog.Handler) *Logger

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

func (*Logger) DebugWithSource

func (l *Logger) DebugWithSource(ctx context.Context, source uintptr, msg string, args ...any)

DebugWithSource logs at `LevelDebug`. The given source will be used instead of the automatically collecting it from the caller.

func (*Logger) Error

func (l *Logger) Error(err error, args ...any)

Error logs the given error at `LevelError`.

func (*Logger) ErrorCtx

func (l *Logger) ErrorCtx(ctx context.Context, err error, args ...any)

ErrorCtx logs the given error at `LevelError` with the given context.

func (*Logger) ErrorWithSource

func (l *Logger) ErrorWithSource(ctx context.Context, source uintptr, err error, args ...any)

ErrorWithSource logs at `LevelError`. The given source will be used instead of the automatically collecting it from the caller.

func (*Logger) InfoWithSource

func (l *Logger) InfoWithSource(ctx context.Context, source uintptr, msg string, args ...any)

InfoWithSource logs at `LevelInfo`. The given source will be used instead of the automatically collecting it from the caller.

func (*Logger) WarnWithSource

func (l *Logger) WarnWithSource(ctx context.Context, source uintptr, msg string, args ...any)

WarnWithSource logs at `LevelWarn`. The given source will be used instead of the automatically collecting it from the caller.

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

With returns a new Logger that includes the given arguments, converted to Attrs as in [Logger.Log]. The Attrs will be added to each output from the Logger. The new Logger shares the old Logger's context. The new Logger's handler is the result of calling WithAttrs on the receiver's handler.

Jump to

Keyboard shortcuts

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