logutil

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package logutil provides various logging utilities that are meant to expand the capabilities of log/slog.

PrettyHandler is a slog.Handler that outputs logs in a text format similar to slog.TextHandler but with pretty formatting and colours. It is intended for use in CLIs to make easy to read logs for users.

MultiHandler is a slog.Handler that allows a single log record to be processed by multiple handlers. It is akin to io.MultiWriter. Each handler has the ability to customize its behaviour.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallerPC

func CallerPC(depth int) uintptr

CallerPC returns the program counter at the given stack depth.

func CallerSource

func CallerSource(pc uintptr) slog.Source

CallerSource returns a slog.Source for the given program counter. If the location is unavailable, it returns a slog.Source with zero fields.

func LogWriter

func LogWriter(logger progress.Logger, level slog.Level) io.WriteCloser

LogWriter returns an io.Writer that can be used to write arbitrary text to the logger. logger is used to log an error if one occurs.

It is the caller's responsibility to close the returned io.WriteCloser in order to free resources.

func RemoveKeys

func RemoveKeys(keys ...string) func([]string, slog.Attr) slog.Attr

RemoveKeys returns a function suitable for HandlerOptions.ReplaceAttr that removes all Attrs with the given keys.

Types

type FormatLogger

type FormatLogger struct {
	*slog.Logger
}

FormatLogger wraps a slog.Logger and gives it Printf-like functions for each log level. It also conforms to the progess.Logger interface.

func NewFormatLogger

func NewFormatLogger(h slog.Handler) *FormatLogger

NewFormatLogger is a convenience function to create a new FormatLogger using a handler.

func (*FormatLogger) Debugf

func (l *FormatLogger) Debugf(format string, args ...any)

func (*FormatLogger) Errorf

func (l *FormatLogger) Errorf(format string, args ...any)

func (*FormatLogger) Infof

func (l *FormatLogger) Infof(format string, args ...any)

func (*FormatLogger) Warnf

func (l *FormatLogger) Warnf(format string, args ...any)

func (*FormatLogger) With

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

func (*FormatLogger) WithAttrs

func (l *FormatLogger) WithAttrs(args ...any) progress.Logger

func (*FormatLogger) WithGroup

func (l *FormatLogger) WithGroup(name string) *FormatLogger

type MultiHandler

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

MultiHandler is a Handler that writes Records to multiple Handlers. It is useful for writing logs to multiple places, such as a file and stdout.

Each handler can be configured independently to have different behaviour. For example, you could have one handler that writes text logs to stdout at info level or higher, while another writes JSON logs to a file at debug level or higher. This would allow for a simpler more human-friendly output on stdout, while still having the full logs available in a file for debugging.

func NewMultiHandler

func NewMultiHandler(handlers []slog.Handler, opts *MultiHandlerOptions) *MultiHandler

NewMultiHandler creates a new MultiHandler that writes to the given handlers, using the given options. If opts is nil, the default options are used.

func (*MultiHandler) Enabled

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

func (*MultiHandler) Handle

func (h *MultiHandler) Handle(ctx context.Context, r slog.Record) error

Handle calls Handle on each handler.

func (*MultiHandler) WithAttrs

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

func (*MultiHandler) WithGroup

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

type MultiHandlerOptions

type MultiHandlerOptions struct {
	// Level reports the minimum record level that will be logged.
	// If nil, the handler assumes slog.LevelDebug in order to allow
	// all handlers to receive the record and decide whether to handle it.
	// This should only be set if you know a certain level will never be used
	// by any handler and want to skip processing of that level.
	Level slog.Leveler
}

type PrettyHandler

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

PrettyHandler is a Handler that writes Records to an io.Writer in a pretty format that looks like so:

DEBUG some log message foo=bar

func NewPrettyHandler

func NewPrettyHandler(w io.Writer, opts *PrettyHandlerOptions) *PrettyHandler

NewPrettyHandler creates a new PrettyHandler that writes to the given writer, using the given options. If opts is nil, the default options are used.

func (*PrettyHandler) Enabled

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

func (*PrettyHandler) Handle

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

func (*PrettyHandler) WithAttrs

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

func (*PrettyHandler) WithGroup

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

type PrettyHandlerOptions

type PrettyHandlerOptions struct {
	// AddSource adds source code position information to the log using
	// the SourceKey attribute.
	AddSource bool

	// Level reports the minimum record level that will be logged.
	// See the Level field of [slog.HandlerOptions].
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// See the ReplaceAttr field of [slog.HandlerOptions].
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr

	// ForceQuote forces quoting of all values.
	// By default, quoting will only be applied if required.
	ForceQuote bool

	// Disables using colours in logs.
	DisableColor bool
}

PrettyHandlerOptions are options for a PrettyHandler. A zero value consists entirely of default values.

type WriterVar

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

WriterVar is a io.Writer variable, to allow a Handler writer to change dynamically. It implements io.Writer as well as a Set method, and is safe for use by multiple goroutines.

A WriterVar must not be copied after first use.

The zero value LevelVar is a no-op writer that discards all data written to it (similar to io.Discard).

func NewWriterVar

func NewWriterVar(w io.Writer) *WriterVar

NewWriterVar creates a new WriterVar with the given writer.

func (*WriterVar) Set

func (v *WriterVar) Set(w io.Writer)

Set sets the underlying writer.

func (*WriterVar) Write

func (v *WriterVar) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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