cmdlog

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 16 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 cmdlog builds the logger of the relay proxy: a slog.Logger writing JSON or logfmt lines to a writer (stderr by default), with the level and the format of the configuration.

Index

Constants

View Source
const (
	// LevelKey is the key of the level of a record.
	LevelKey = "level"
	// TimeKey is the key of the time of a record, in seconds since the epoch.
	TimeKey = "ts"
	// CallerKey is the key of the "dir/file.go:line" that logged the record.
	CallerKey = "caller"
	// MessageKey is the key of the message of a record.
	MessageKey = "msg"
	// ErrorKey is the key of the attribute made by [Err].
	ErrorKey = "error"
)

Output keys.

View Source
const LevelFatal = slog.Level(12)

LevelFatal is the level of the messages logged right before the process exits.

View Source
const RedactedValue = "[REDACTED]"

RedactedValue is what a Secret prints instead of its content.

Variables

View Source
var ErrUnknownFormat = errors.New("unknown log format")

ErrUnknownFormat is returned for a log format that is neither json nor logfmt.

View Source
var ErrUnrecognizedLevel = errors.New("unrecognized level")

ErrUnrecognizedLevel is returned for a log level that is not one of the supported names.

Functions

func Err

func Err(err error) slog.Attr

Err is the attribute of an error, under the "error" key. A nil error gives an empty attribute, which the handlers skip.

func ParseLevel

func ParseLevel(name string) (slog.Level, error)

ParseLevel converts a configured log level. It accepts what zap accepted (zapcore.Level.UnmarshalText): the names debug, info (also empty), warn (also warning), error, dpanic, panic and fatal, in any letter case. dpanic, panic and fatal are above the error level, as in zap: only the records of the fatal level pass.

func SecretAttr

func SecretAttr(key, value string) slog.Attr

SecretAttr is a log attribute holding a secret: only its redacted form is logged.

Types

type Format

type Format int32

Format is an output format of the handler.

const (
	// FormatJSON writes one JSON object per line.
	FormatJSON Format = iota
	// FormatLogfmt writes one line of key=value pairs per record.
	FormatLogfmt
)

func ParseFormat

func ParseFormat(name string) (Format, error)

ParseFormat converts a configured log format.

type Logger

type Logger struct {
	// Slog is the logger to pass to the code that logs.
	Slog *slog.Logger
	// contains filtered or unexported fields
}

Logger is the logger of the process, built from the log level and the log format. The handlers of every logger derived from Logger.Slog (With, WithGroup) follow Logger.Update.

func New

func New(format, level string, opts ...Option) (*Logger, error)

New builds a logger. An empty format is json, an empty level is info.

func NewDefault

func NewDefault(opts ...Option) *Logger

NewDefault builds the logger that is used until the configuration is read: json lines at the info level on stderr.

func (*Logger) Update

func (l *Logger) Update(format, level string) error

Update changes the format and the level of the logger, and of every logger derived from it. It is safe for concurrent use. When it fails nothing changes.

type Option

type Option func(*options)

Option configures the logger.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets where the logger writes. The default is stderr. Writes are serialised by the logger, the writer does not need to be safe for concurrent use.

type Secret

type Secret string

Secret is a string that must never reach the logs (API key, token, connection string, ...). Whatever the handler or the formatting verb, it only ever prints RedactedValue.

func (Secret) GoString

func (Secret) GoString() string

GoString implements fmt.GoStringer, so "%#v" never leaks the secret.

func (Secret) LogValue

func (Secret) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (Secret) MarshalText

func (Secret) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler, so JSON and YAML encoders never leak the secret.

func (Secret) String

func (Secret) String() string

String implements fmt.Stringer, so "%v" and "%s" never leak the secret.

Jump to

Keyboard shortcuts

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