Documentation
¶
Overview ¶
Package logging provides structured logging for the aix CLI using slog.
The package supports both text and JSON output formats, configurable log levels, and helpers for testing. All loggers are based on the standard library's log/slog package.
Basic Usage ¶
logger := logging.New(logging.Config{
Level: slog.LevelInfo,
Format: logging.FormatText,
Output: os.Stderr,
})
logger.Info("starting", "version", "1.0.0")
Testing ¶
For tests, use ForTest to capture log output via the testing framework:
func TestSomething(t *testing.T) {
logger := logging.ForTest(t)
// logs appear in test output on failure
}
Quiet Mode ¶
Use NewDiscard when log output should be suppressed entirely:
logger := logging.NewDiscard()
Configuration Precedence ¶
Logging is configured in the following order of precedence:
- CLI Flags: --verbose/-v, --quiet/-q, --log-format, --log-file.
- Environment Variables: AIX_DEBUG=1 (Debug), AIX_DEBUG=2 (Trace).
- Defaults: Warn level, Text format, Output to Stderr.
Note that --log-file always uses JSON format regardless of --log-format. --log-format only controls the primary output (Stderr).
Index ¶
- Constants
- func Default() *slog.Logger
- func ForTest(t *testing.T) *slog.Logger
- func FromContext(ctx context.Context) *slog.Logger
- func IsTTY(w io.Writer) bool
- func LevelFromVerbosity(v int) slog.Level
- func New(cfg Config) *slog.Logger
- func NewContext(ctx context.Context, l *slog.Logger) context.Context
- func NewDiscard() *slog.Logger
- func SupportsColor(w io.Writer) bool
- type Config
- type Format
- type Handler
- type MultiHandler
Constants ¶
const LevelTrace = slog.LevelDebug - 4
LevelTrace is the log level for trace messages. It is lower than Debug (-4) to allow for very verbose output.
Variables ¶
This section is empty.
Functions ¶
func Default ¶
Default returns a sensible default logger configured for CLI use. It logs at Warn level in text format to stderr, matching the CLI's default verbosity.
func ForTest ¶
ForTest creates a logger that writes to the test's log output. Log messages appear only when the test fails or when running with -v. The logger is configured at Debug level to capture all messages.
func FromContext ¶
FromContext returns the logger stored in the context, or the default logger if none is set.
func IsTTY ¶
IsTTY returns true if the given writer is a terminal. It supports os.File and any wrapper that provides an Fd() method.
func LevelFromVerbosity ¶
LevelFromVerbosity returns the log level for the given verbosity count. 0 -> Warn 1 -> Info 2 -> Debug 3+ -> Trace
func New ¶
New creates a logger with the given configuration. If cfg.Output is nil, it defaults to os.Stderr. If cfg.Format is not recognized, it defaults to FormatText.
func NewContext ¶
NewContext returns a new context with the given logger attached.
func NewDiscard ¶
NewDiscard creates a logger that discards all output. Use this for quiet mode or when logging should be suppressed.
func SupportsColor ¶
SupportsColor returns true if the given writer supports ANSI color codes. It returns false if:
- The writer is not a TTY
- The NO_COLOR environment variable is set
- The TERM environment variable is set to "dumb"
Types ¶
type Config ¶
type Config struct {
// Level sets the minimum log level. Messages below this level are discarded.
Level slog.Level
// Format specifies the output format (text or JSON).
Format Format
// Output is where log messages are written. Defaults to os.Stderr if nil.
Output io.Writer
}
Config holds the configuration for creating a new logger.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler implements slog.Handler for TTY-optimized text output. It provides colorized output when the writer supports it.
func NewHandler ¶
func NewHandler(out io.Writer, opts *slog.HandlerOptions) *Handler
NewHandler creates a new TTY-optimized text handler.
type MultiHandler ¶
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler dispatches records to multiple handlers.
func NewMultiHandler ¶
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler
NewMultiHandler creates a new MultiHandler that dispatches to all provided handlers.
func (*MultiHandler) Enabled ¶
Enabled reports whether at least one of the underlying handlers is enabled for the given level.