logx

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 (redact.Patterns) before it is written.

The handler depends on term and redact only, so a tool that logs does not compile in the keyring and prompt code of the secret package.

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 redact.Patterns, 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 redact.Values 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"

func (*LineHandler) Enabled

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

Enabled reports whether a record at level is written.

func (*LineHandler) Handle

func (h *LineHandler) Handle(_ context.Context, record slog.Record) error

Handle writes the record as one line.

func (*LineHandler) WithAttrs

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

WithAttrs returns a handler that writes attrs, in the groups h has, in front of the attributes of every record.

func (*LineHandler) WithGroup

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

WithGroup returns a handler that puts the attributes added after it into the group name. An empty name returns h, as slog asks.

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.

Jump to

Keyboard shortcuts

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