Documentation
¶
Overview ¶
Package logx holds log/slog handlers for command-line tools.
LineHandler writes each record as one plain line, the message and then its attributes as key=value, which is what a --verbose flag shows on the error stream. A log line often carries text from elsewhere, such as a server's error message or a request URL, so every line is made plain text (term.Plain), and credentials it recognizes by their shape are replaced (secret.RedactPatterns) before it is written.
The handler lives in a package of its own because it needs both term and secret, and secret already depends on term.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LineHandler ¶
type LineHandler struct {
// contains filtered or unexported fields
}
LineHandler is a slog.Handler that writes each record as one plain line: the message, then the attributes as key=value, separated by spaces, with neither time nor level:
sent a request method=GET path=/v2/projects status=200
The attributes of LineHandler.WithAttrs come first, then those of the record. An attribute in a group, from LineHandler.WithGroup or a group value, has the group's name in front of its key, separated by a dot, as slog.TextHandler writes it. Keys and values are written as slog.Value.String gives them, made plain text with term.Plain, and in double quotes, as strconv.Quote writes them, when they hold anything but letters, digits and -._/:@+%~ — white space, a newline or an equals sign, for example. A value therefore stays on its line, and where it ends is never in doubt.
The message goes through term.Plain as well. Then the message and each key=value pair go through secret.RedactPatterns, each on its own, so that text in one cannot hide a credential in another: escape sequences and control characters but the newline are taken out, and recognizable credentials such as password=… or Bearer tokens are replaced, a quoted value as a whole. A message with a newline still spans several lines. The handler does not know the secrets the program holds; pass those through secret.Redact before they reach a log record.
A LineHandler and the handlers derived from it share one lock, so lines written from several goroutines do not interleave.
func NewLineHandler ¶
func NewLineHandler(w io.Writer, opts *Options) *LineHandler
NewLineHandler returns a LineHandler that writes to w. opts may be nil.
Example ¶
package main
import (
"log/slog"
"os"
"github.com/mittwald/cli-kit/logx"
)
func main() {
logger := slog.New(logx.NewLineHandler(os.Stdout, &logx.Options{Level: slog.LevelDebug}))
request := logger.WithGroup("request").With("method", "GET")
request.Debug("sent", "path", "/v2/projects", "status", 200)
logger.Warn("login failed", "body", "password=hunter2 \x1b[31mdenied\x1b[0m")
}
Output: sent request.method=GET request.path=/v2/projects request.status=200 login failed body="password=[REDACTED] denied"
type Options ¶
type Options struct {
// Level is the lowest level that is written; nil means slog.LevelInfo,
// as for the handlers of log/slog. A --verbose flag usually sets
// slog.LevelDebug.
Level slog.Leveler
}
Options configures a LineHandler.