Documentation
¶
Overview ¶
Package logging wires the nebula runtime-reconfigurable slog handler used by nebula.Main and the nebula CLI binaries. Callers build a logger with NewLogger, then call ApplyConfig at startup and from a config reload callback to push logging.level, logging.format, and logging.disable_timestamp changes onto the logger without rebuilding it.
Index ¶
- Constants
- func ApplyConfig(l *slog.Logger, c Config) error
- func LevelName(l slog.Level) string
- func NewLogger(w io.Writer) *slog.Logger
- func ParseLevel(s string) (slog.Level, error)
- type Config
- type Handler
- func (h *Handler) Enabled(_ context.Context, l slog.Level) bool
- func (h *Handler) GetFormat() string
- func (h *Handler) GetLevel() slog.Level
- func (h *Handler) Handle(ctx context.Context, r slog.Record) error
- func (h *Handler) SetDisableTimestamp(v bool)
- func (h *Handler) SetFormat(format string) error
- func (h *Handler) SetLevel(level slog.Level)
- func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *Handler) WithGroup(name string) slog.Handler
Constants ¶
const LevelTrace = slog.Level(-8)
LevelTrace is a custom slog level below Debug, used when logging.level is "trace". slog has no builtin trace level; the value is one step below slog.LevelDebug in slog's 4-point spacing.
Variables ¶
This section is empty.
Functions ¶
func ApplyConfig ¶
ApplyConfig reads logging.level, logging.format, and (optionally) logging.disable_timestamp from c and applies them to l. The reconfig surface is discovered via structural type-assertion on l.Handler(), so foreign handlers silently opt out of whichever capabilities they do not implement.
nebula.Main does NOT call this function on your behalf; callers that want config-driven log level / format / timestamp updates invoke it at startup and register it as a reload callback themselves. This keeps the library from mutating an embedder's logger without their say-so.
func LevelName ¶
LevelName returns a human-readable name for a slog.Level matching the strings accepted by ParseLevel.
func NewLogger ¶
NewLogger returns a *slog.Logger whose level, format, and timestamp emission can be reconfigured at runtime via ApplyConfig and the SSH debug commands. The default configuration is info-level text output so log calls made before ApplyConfig runs still produce output. Timestamps follow slog's default RFC3339Nano format; set logging.disable_timestamp in config to suppress them.
ApplyConfig and the SSH commands discover the reconfig surface via structural type-assertion on l.Handler(), so replacement implementations (tests, platform-specific sinks) need only implement the subset of {SetLevel(slog.Level), SetFormat(string) error, SetDisableTimestamp(bool)} they care about. Callers that pass a plain *slog.Logger without these methods get a silent no-op; reconfiguration is always opt-in.
func ParseLevel ¶
ParseLevel converts a config-string level name ("trace", "debug", "info", "warn"/"warning", "error", "fatal"/"panic") to a slog.Level. "fatal" and "panic" are accepted for backwards compatibility with pre-slog configs and both map to slog.LevelError.
Types ¶
type Config ¶
Config is the subset of *config.C that ApplyConfig reads. Declaring it here keeps the logging package from depending on config directly, which would cycle through the shared test helpers (test.NewLogger imports logging, and config's tests import test). *config.C satisfies this interface structurally with no adapter.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is the slog.Handler returned by NewHandler. It holds two pre-derived slog handlers -- one text, one json -- both built from the same accumulated WithAttrs/WithGroup state. Handle picks which one to dispatch to based on handlerRoot.jsonMode, so a SetFormat call takes effect immediately across the whole process without having to rebuild any derived loggers.
func NewHandler ¶
NewHandler builds the *Handler that NewLogger wraps. Exported for platform-specific sinks (notably cmd/nebula-service/logs_windows.go) that want to wrap the handler with extra behavior, such as tagging each record with its Event Log severity, while still benefiting from all the level / format / timestamp / WithAttrs machinery implemented here.
func (*Handler) SetDisableTimestamp ¶
SetDisableTimestamp toggles whether Handle zeroes r.Time before dispatching (slog's builtin text/json handlers skip emitting the time attribute on a zero time).
func (*Handler) SetFormat ¶
SetFormat flips the output format atomically. Valid formats are "text" and "json". Every derived logger sees the new format on its next Handle call; no rebuild or registration is required.