logging

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const RedactedPlaceholder = "***"

RedactedPlaceholder is the value substituted for any non-empty secret when a redacted view of a config is produced.

Variables

View Source
var DefaultSecretFields = []string{
	"password",
	"token",
	"auth-token",
	"client-secret",
	"clientsecret",
	"bearer",
	"secret",
	"api-key",
	"apikey",
	"credentials",
}

DefaultSecretFields lists JSON keys that are treated as secrets by RedactedJSON when no explicit list is provided. Matching is case-insensitive against the lowercase form of the key.

Functions

func Component added in v0.46.0

func Component(parent *slog.Logger, kind, value, name string) *slog.Logger

Component returns a child logger annotated with the component kind and instance name. If parent is nil, a discard logger is returned so callers can use the result unconditionally.

The kind argument is used both as the attribute key and the attribute value, e.g. Component(l, "output", "file", "f1") yields output=file name=f1. Common kinds: "output", "input", "processor", "cache", "locker", "loader", "action".

func DiscardLogger added in v0.46.0

func DiscardLogger() *slog.Logger

DiscardLogger returns a slog.Logger that discards all records.

func GetLogger

func GetLogger(level slog.Level, args ...any) *slog.Logger

func NewLogger

func NewLogger(store store.Store[any], args ...any) *slog.Logger

func NewTest added in v0.46.0

func NewTest(t TestLogf) *slog.Logger

NewTest returns a slog.Logger that forwards every record to t.Logf at LevelDebug or above. Useful for tests that want logger output captured alongside other test output.

func Redact added in v0.46.0

func Redact(s string) string

Redact returns "***" when s is non-empty, and "" otherwise. Centralising this lets us change the placeholder in one place and avoids accidental "***" leakage when a secret was never set.

func RedactedJSON added in v0.46.0

func RedactedJSON(v any, fields ...string) string

RedactedJSON marshals v to JSON and replaces the values of any string fields whose key (case-insensitive) appears in fields with "***". If fields is empty, DefaultSecretFields is used. Non-string values keyed by a secret name are left untouched (this avoids surprising shape changes for typed numeric or bool fields). On error, an empty string is returned so callers can use it directly in log lines.

func RedactedValue added in v0.46.0

func RedactedValue(v any, fields ...string) slog.Value

RedactedValue returns v's JSON representation with configured secret fields redacted. Types that can contain credentials can use this from LogValue to make slog.Any("config", v) safe by default.

func StdLogger added in v0.46.0

func StdLogger(l *slog.Logger, level slog.Level) *log.Logger

StdLogger returns a *log.Logger that writes through l's Handler using level as the record level.

Types

type Flags added in v0.46.0

type Flags struct {
	Log           bool
	Debug         bool
	LogFile       string
	LogMaxSize    int
	LogMaxBackups int
	LogCompress   bool
}

Flags carries the subset of configuration values needed to build the application logger. It is a value type so callers can construct it directly without sharing pointers.

type FlagsProvider added in v0.46.0

type FlagsProvider interface {
	LoggingFlags() Flags
}

FlagsProvider is implemented by types that expose logging Flags. Configuration types implement this interface so the logging package can construct a logger without depending on the config package.

type TestLogf added in v0.46.0

type TestLogf interface {
	Logf(format string, args ...any)
}

TestLogf is the subset of testing.TB used by NewTest. Defined as an interface so this package does not depend on the testing package and so callers can plug in their own sinks.

Jump to

Keyboard shortcuts

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