Documentation
¶
Overview ¶
Package logging installs a styled *slog.Logger via nabat.WithExtension, with optional --verbose / --log-level flags. ParseLevel accepts the same level names. For a custom logger, use nabat.WithLogger instead.
app := nabat.MustNew("myctl",
nabat.WithExtension(logging.New(
logging.WithLevel(slog.LevelInfo),
logging.WithVerboseFlag("verbose"),
)),
)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilOption is returned when a nil [Option] is passed to [New]. ErrNilOption = errors.New("nabat/logging: option is nil") // ErrNilHandler is returned when [WithHandler] receives a nil handler. ErrNilHandler = errors.New("nabat/logging: handler is nil") // ErrUnknownLevel is returned when [ParseLevel] does not recognize // the input string. ErrUnknownLevel = errors.New("nabat/logging: unknown log level") )
Sentinel errors for errors.Is checks.
Functions ¶
func New ¶
New returns a nabat.Extension that installs a styled logger. A nil option returns an error wrapping ErrNilOption.
Example ¶
package main
import (
"context"
"fmt"
"strings"
"nabat.dev/logging"
"nabat.dev/nabat"
"nabat.dev/nabat/nabattest"
)
func main() {
io, _, _, stderr := nabattest.NewIO()
app := nabat.MustNew("myctl",
nabat.WithIO(io),
nabat.WithExtension(logging.New(logging.WithVerboseFlag("verbose"))),
)
app.MustCommand("hello", nabat.WithRun(func(c *nabat.Context) error {
c.Logger().Info("ready")
return nil
}))
if err := app.RunArgs(context.Background(), "hello"); err != nil {
fmt.Println("error:", err)
return
}
fmt.Print(strings.TrimSpace(stderr.String()))
}
Output: INFO ready
func ParseLevel ¶
ParseLevel parses a log level name (case-insensitive): debug, info, warn, error. On failure, the error wraps ErrUnknownLevel.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a styled slog.Handler that writes human-readable lines using a Styles value (typically from FromTheme). Output format is LEVL message plus optional key=value pairs. Safe for concurrent use, including handlers derived via Handler.WithAttrs and Handler.WithGroup, which share the writer mutex.
func NewHandler ¶
func NewHandler(w io.Writer, opts HandlerOptions) *Handler
NewHandler returns a Handler that writes styled log output to w.
Panics if w is nil: a handler without a writer is a programmer error.
func (*Handler) SetStyles ¶
SetStyles updates the handler's styles. Safe to call concurrently with log writes.
type HandlerOptions ¶
type HandlerOptions struct {
// Level is the minimum enabled level. When nil, [NewHandler] installs a
// fresh [slog.LevelVar] at [slog.LevelInfo] (zero value of LevelVar).
Level *slog.LevelVar
// Styles controls level badges and key/value coloring. The zero value
// renders without lipgloss styling.
Styles Styles
// Timestamp, when true, prefixes each line with a Kitchen-format clock.
Timestamp bool
}
HandlerOptions configures a Handler.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures the logging extension.
func WithHandler ¶
WithHandler installs a custom slog handler. The extension wraps it so verbose and level flag changes still apply. Returns an error if h is nil.
func WithLevel ¶
WithLevel sets the base log level (default slog.LevelInfo).
func WithLevelFlag ¶
WithLevelFlag wires a string flag (debug|info|warn|error) that sets the per-invocation level. The flag must be declared via nabat.WithFlag.
func WithSetDefault ¶
func WithSetDefault() Option
WithSetDefault installs the extension's logger as the process-wide slog.Default via slog.SetDefault.
func WithTimestamp ¶
func WithTimestamp() Option
WithTimestamp enables timestamps on the default styled handler. Has no effect when WithHandler supplies a custom handler.
func WithVerboseFlag ¶
WithVerboseFlag wires a bool flag that sets slog.LevelDebug when explicitly set to true. The flag must be declared via nabat.WithFlag (typically on the root with nabat.WithPersistent).
type Styles ¶
type Styles struct {
// Debug styles the "DEBU" level badge.
Debug lipgloss.Style
// Info styles the "INFO" level badge.
Info lipgloss.Style
// Warn styles the "WARN" level badge.
Warn lipgloss.Style
// Error styles the "ERRO" level badge.
Error lipgloss.Style
// Key styles structured-log attribute keys.
Key lipgloss.Style
// Value styles structured-log attribute values.
Value lipgloss.Style
}
Styles holds lipgloss styles for level badges and key/value pairs in structured log output. Build with FromTheme or by hand. Once built, Styles is immutable and safe to share across goroutines.
func FromTheme ¶
func FromTheme(rt theme.ResolvedTheme) Styles
FromTheme derives Styles from a theme.ResolvedTheme using theme.StatusInfo, theme.StatusWarning, and theme.StatusError for badges, and theme.AccentPrimary / theme.TextPrimary for key=value pairs. Badges are fixed-width "DEBU" / "INFO" / "WARN" / "ERRO" labels rendered bold. Callers that need a different shape can build Styles directly.