logging

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 7 Imported by: 0

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

View Source
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

func ApplyConfig(l *slog.Logger, c Config) error

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

func LevelName(l slog.Level) string

LevelName returns a human-readable name for a slog.Level matching the strings accepted by ParseLevel.

func NewLogger

func NewLogger(w io.Writer) *slog.Logger

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

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

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

type Config interface {
	GetString(key, def string) string
	GetBool(key string, def bool) bool
}

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

func NewHandler(w io.Writer) *Handler

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) Enabled

func (h *Handler) Enabled(_ context.Context, l slog.Level) bool

func (*Handler) GetFormat

func (h *Handler) GetFormat() string

GetFormat reports the currently selected format name.

func (*Handler) GetLevel

func (h *Handler) GetLevel() slog.Level

GetLevel reports the current log level.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

func (*Handler) SetDisableTimestamp

func (h *Handler) SetDisableTimestamp(v bool)

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

func (h *Handler) SetFormat(format string) error

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.

func (*Handler) SetLevel

func (h *Handler) SetLevel(level slog.Level)

SetLevel updates the effective log level. Propagates to every derived logger via the shared LevelVar.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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