logger

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package logger provides the dhctl logging foundation built on log/slog. It exposes a dual-sink handler (file always; terminal for ShowInCompacted()-tagged records), a RunProcess helper for process blocks, silent/buffer loggers, an io.Writer adapter, klog and shell-operator bindings, a root-logger constructor, and the single LibdhctlAdapter bridging slog to lib-connection's logger interface.

Index

Constants

View Source
const DeckhouseBanner = "" +
	`========================================================================================
 _____             _     _                                ______                _ _____
(____ \           | |   | |                              / _____)              | (_____)
 _   \ \ ____ ____| |  _| | _   ___  _   _  ___  ____   | /      ____ ____   _ | |  _
| |   | / _  ) ___) | / ) || \ / _ \| | | |/___)/ _  )  | |     / _  |  _ \ / || | | |
| |__/ ( (/ ( (___| |< (| | | | |_| | |_| |___ ( (/ /   | \____( ( | | | | ( (_| |_| |_
|_____/ \____)____)_| \_)_| |_|\___/ \____(___/ \____)   \______)_||_|_| |_|\____(_____)
========================================================================================`

DeckhouseBanner is the startup ASCII banner shown at the top of dhctl operations (bootstrap, destroy, ...). Kept here so every operation shares one source of truth.

Variables

This section is empty.

Functions

func BadgeFailed

func BadgeFailed() slog.Attr

func BadgeSuccess

func BadgeSuccess() slog.Attr

BadgeSuccess/BadgeFailed/BadgeWarning return the attribute tagging a record to render with the matching colored status badge. Pair with ShowInCompacted() so the record reaches the compact view.

func BadgeWarning

func BadgeWarning() slog.Attr
func Banner() slog.Attr

Banner marks a record whose message is the startup ASCII banner. The terminal UI pins it at the top of the live canvas instead of scrolling it as a log line.

func BindKlog

func BindKlog(l *slog.Logger)

BindKlog routes k8s.io/klog/v2 output into the given slog logger. Sensitive-keyword sanitization happens in the handler (via Sanitize), so klog output is filtered too.

func BindShellOperator

func BindShellOperator(l *slog.Logger, debug bool)

BindShellOperator routes shell-operator's default logger output into the given slog logger.

shell-operator uses github.com/deckhouse/deckhouse/pkg/log as its logger and emits klog/internal lines through it. When debug is false, shell-operator is muted (fatal level); when debug is true, its level is lowered to debug and its output is captured into l via a file-only LineWriter, so each emitted line becomes an slog record that enriches the .log file but never reaches the terminal. That keeps the terminal identical with or without DHCTL_DEBUG.

Note: this mutates the shell-operator global default logger (shlog.Default()).

func ConnectionString

func ConnectionString() slog.Attr

ConnectionString marks a record whose message is the SSH connection string. The terminal UI pins it as a distinct milestone so it stays visible and is included in the closing summary.

func Discard

func Discard() *slog.Logger

Discard returns a logger that drops every record. Replaces NewSilentLogger / NewDummyLogger.

func Fail

func Fail(ctx context.Context, l *slog.Logger, msg string)

Fail logs a failure line at Error level, so it is always visible (compact and verbose).

func FailRetry

func FailRetry(ctx context.Context, l *slog.Logger, msg string)

FailRetry logs a retryable failure at Warn level, so it is always visible.

func FileOnly

func FileOnly() slog.Attr

FileOnly tags a record to stay file-only on the terminal: suppressed in the compact view regardless of level, shown only with -v.

func FinishProgress

func FinishProgress(ctx context.Context, l *slog.Logger)

FinishProgress closes the active terminal progress session.

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext returns the logger carried by ctx, or slog.Default() if none is present. It never returns nil.

func JSON

func JSON(ctx context.Context, l *slog.Logger, data []byte)

JSON writes a raw JSON payload as a file record (not TTY-tagged).

func NewBufferLogger

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

NewBufferLogger returns a logger writing every record to w as text. Replaces BufferLogger.

func NewRoot

func NewRoot(opts Options) *slog.Logger

NewRoot builds the application root logger. Replaces InitLogger / InitLoggerWithOptions / WrapWithTeeLogger / NewLogToFile from the old package.

func NewStreamLogger

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

NewStreamLogger returns a logger that renders the compact UI (process boxes framed with ┌/│/└, milestones, banner, connection string) as plain ANSI-free lines to w — the format the commander client expects, instead of raw slog text. It backs the gRPC client-stream logger.

Unlike NewRoot it does not register a global rootHandler, so it is safe to build per request and for concurrent operations. There is no file (JSON) sink: w is a server LogWriter that already logs every rendered line to the server slog and forwards it to the client. verbose forwards every Info+ detail line (e.g. terraform output) to the renderer; DEBUG never reaches the terminal sink.

func PauseProgress

func PauseProgress(ctx context.Context, l *slog.Logger)

PauseProgress stops rendering the progress session (e.g. around interactive input).

func PrintBanner

func PrintBanner(ctx context.Context)

PrintBanner logs the Deckhouse ASCII banner. Tagged Banner(): the terminal UI pins it at the top of the live canvas instead of scrolling it as an ordinary log line.

func ProcessEnd added in v0.20.0

func ProcessEnd(ctx context.Context, l *slog.Logger, name string)

func ProcessFailed added in v0.20.0

func ProcessFailed(ctx context.Context, l *slog.Logger, name string)

func ProcessStart added in v0.20.0

func ProcessStart(ctx context.Context, l *slog.Logger, name string)

func Progress

func Progress(ctx context.Context, l *slog.Logger, frac float64, title string)

Progress advances the active bar to frac (0..1) and sets its title.

func RestoreTerminal

func RestoreTerminal()

RestoreTerminal leaves the alternate screen if the current root handler is using an interactive Block. Safe to call before NewRoot, multiple times, and after the Block has already been finished. Designed as a no-arg shutdown hook / defer (see cmd/dhctl/main.go).

func ResumeProgress

func ResumeProgress(ctx context.Context, l *slog.Logger)

ResumeProgress restarts rendering the progress session after a pause.

func RunProcess

func RunProcess(ctx context.Context, l *slog.Logger, name string, fn func(context.Context) error) error

RunProcess wraps fn with process start/end (or start/fail) marker records, so the handler can render a process block. The returned error is fn's error, unchanged.

func Sanitize

func Sanitize(_ []string, attr slog.Attr) slog.Attr

Sanitize is a slog.ReplaceAttr function. It scans string values for sensitive keywords and replaces matches with [FILTERED - ...]. Recursively processes groups. Skips control attributes (time, level, source, renderer markers). Idempotent - already filtered values pass through unchanged.

func ShowInCompacted

func ShowInCompacted() slog.Attr

ShowInCompacted returns the attribute that tags a record to appear in the compact view.

func StartProgress

func StartProgress(ctx context.Context, l *slog.Logger, name string)

StartProgress opens a terminal progress session named name.

func Success

func Success(ctx context.Context, l *slog.Logger, msg string)

Success logs a success line. It is NOT tagged for the compact view — only successful PHASE transitions appear there (emitted by the phases progress consumer). Generic successes are verbose-only.

func ToContext

func ToContext(ctx context.Context, l *slog.Logger) context.Context

ToContext returns a copy of ctx carrying the given logger.

Types

type LineWriter

type LineWriter struct {
	// contains filtered or unexported fields
}

LineWriter is an io.Writer that turns each newline-terminated line into an Info record. Used to pipe external command (exec) output into the logger. Replaces logger-as-io.Writer. When fileOnly is set, each line is tagged FileOnly so it lands in the debug file but never the compact terminal.

func NewLineWriter

func NewLineWriter(l *slog.Logger) *LineWriter

NewLineWriter returns a LineWriter emitting each line as a plain Info record.

func (*LineWriter) Write

func (w *LineWriter) Write(p []byte) (int, error)

type Options

type Options struct {
	FileWriter io.Writer // required; always-on sink
	TTYWriter  io.Writer // optional; terminal or plain non-TTY output sink
	IsTTY      bool      // whether TTYWriter is connected to an interactive terminal
	// Interactive enables the pinned pterm progress bar when TTYWriter is connected
	// to a real terminal. Otherwise, the sink renders plain linear output.
	Interactive bool
	// Verbose (-v) shows every Info+ record in the output stream,
	// not just curated compact output.
	Verbose bool
}

Options configures the root logger.

  • FileWriter receives every record (the always-on debug-file sink). Required.
  • TTYWriter, when non-nil, receives terminal or plain non-TTY output.
  • IsTTY determines whether TTYWriter supports interactive terminal rendering.

type TerminalUIHandler

type TerminalUIHandler struct {
	// contains filtered or unexported fields
}

TerminalUIHandler is a dual-sink slog.Handler.

  • File sink: receives every enabled record (JSON).
  • Output sink, when configured, uses interactive terminal rendering for TTYs and plain linear rendering for non-TTY consumers.

func (*TerminalUIHandler) Enabled

func (h *TerminalUIHandler) Enabled(_ context.Context, level slog.Level) bool

func (*TerminalUIHandler) Handle

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

func (*TerminalUIHandler) RestoreTerminal

func (h *TerminalUIHandler) RestoreTerminal()

RestoreTerminal leaves the alternate screen if an interactive Block is active. Safe to call multiple times and when no Block exists.

func (*TerminalUIHandler) WithAttrs

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

func (*TerminalUIHandler) WithGroup

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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