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 ¶
- func CallerPC(depth int) uintptr
- func CallerSource(pc uintptr) slog.Source
- func LogWriter(logger progress.Logger, level slog.Level) io.WriteCloser
- func RemoveKeys(keys ...string) func([]string, slog.Attr) slog.Attr
- type FormatLogger
- func (l *FormatLogger) Debugf(format string, args ...any)
- func (l *FormatLogger) Errorf(format string, args ...any)
- func (l *FormatLogger) Infof(format string, args ...any)
- func (l *FormatLogger) Warnf(format string, args ...any)
- func (l *FormatLogger) With(args ...any) *FormatLogger
- func (l *FormatLogger) WithAttrs(args ...any) progress.Logger
- func (l *FormatLogger) WithGroup(name string) *FormatLogger
- type MultiHandler
- type MultiHandlerOptions
- type PrettyHandler
- type PrettyHandlerOptions
- type WriterVar
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallerSource ¶
CallerSource returns a slog.Source for the given program counter. If the location is unavailable, it returns a slog.Source with zero fields.
Types ¶
type FormatLogger ¶
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) 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.
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.
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 ¶
NewWriterVar creates a new WriterVar with the given writer.