logger

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: Apache-2.0 Imports: 15 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 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 Adapter

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

Adapter wraps slog.Logger and implements libdhctl_log.LoggerProvider and its internal interfaces.

func NewAdapter

func NewAdapter(logger *slog.Logger) *Adapter

NewAdapter creates a new logger adapter.

func NewLibdhctlAdapter

func NewLibdhctlAdapter(ctx context.Context) *Adapter

NewLibdhctlAdapter is the canonical constructor used at the lib-connection boundary. It binds ctx so every record the adapter emits carries the caller's context (cancellation, trace span, context values) instead of a detached background one.

func (*Adapter) BufferLogger

func (a *Adapter) BufferLogger(buffer *bytes.Buffer) libdhctl_log.Logger

BufferLogger returns an Adapter writing every record to buffer as sanitized JSON. The buffer is never a terminal, so this is a plain file-style sink (no progress UI).

func (*Adapter) DebugF

func (a *Adapter) DebugF(format string, args ...interface{})

func (*Adapter) DebugFWithoutLn

func (a *Adapter) DebugFWithoutLn(format string, args ...interface{})

func (*Adapter) DebugLn

func (a *Adapter) DebugLn(args ...interface{})

func (*Adapter) ErrorF

func (a *Adapter) ErrorF(format string, args ...interface{})

func (*Adapter) ErrorFWithoutLn

func (a *Adapter) ErrorFWithoutLn(format string, args ...interface{})

func (*Adapter) ErrorLn

func (a *Adapter) ErrorLn(args ...interface{})

func (*Adapter) Fail

func (a *Adapter) Fail(msg string)

func (*Adapter) FailRetry

func (a *Adapter) FailRetry(msg string)

func (*Adapter) FlushAndClose

func (a *Adapter) FlushAndClose() error

FlushAndClose implements libdhctl_log.Logger.

func (*Adapter) InfoF

func (a *Adapter) InfoF(format string, args ...interface{})

func (*Adapter) InfoFWithoutLn

func (a *Adapter) InfoFWithoutLn(format string, args ...interface{})

func (*Adapter) InfoLn

func (a *Adapter) InfoLn(args ...interface{})

func (*Adapter) JSON

func (a *Adapter) JSON(data []byte)

func (*Adapter) Process

func (a *Adapter) Process(p libdhctl_log.Process, t string, run func() error) error

Process implements libdhctl_log.Logger.

func (*Adapter) ProcessLogger

func (a *Adapter) ProcessLogger() libdhctl_log.ProcessLogger

ProcessLogger implements libdhctl_log.Logger by returning a ProcessLogger bound to this adapter.

func (*Adapter) SilentLogger

func (a *Adapter) SilentLogger() *libdhctl_log.SilentLogger

func (*Adapter) Success

func (a *Adapter) Success(msg string)

Success and FailRetry are per-process / per-attempt lib-connection notices, emitted in bulk (each dependency check, each retry). They are NOT compact-tagged: they stay in the debug file and only surface with -v. Only successful PHASE transitions appear in the compact view (see pkg/operations/phases). FailRetry is retry noise — logged at Debug so a recovered retry stays quiet. Fail is a real failure: it surfaces by its Error level and renders the red FAILED badge.

func (*Adapter) WarnF

func (a *Adapter) WarnF(format string, args ...interface{})

func (*Adapter) WarnFWithoutLn

func (a *Adapter) WarnFWithoutLn(format string, args ...interface{})

func (*Adapter) WarnLn

func (a *Adapter) WarnLn(args ...interface{})

func (*Adapter) Warning

func (a *Adapter) Warning(msg string)

func (*Adapter) WithContext

func (a *Adapter) WithContext(ctx context.Context) *Adapter

WithContext returns a new Adapter with the given context.

func (*Adapter) Write

func (a *Adapter) Write(p []byte) (int, error)

type AdapterProcessLogger

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

AdapterProcessLogger bridges lib-connection's ProcessLogger to the process-block markers the terminal handler renders (┌ … └), mirroring RunProcess.

func (*AdapterProcessLogger) ProcessEnd

func (ap *AdapterProcessLogger) ProcessEnd()

func (*AdapterProcessLogger) ProcessFail

func (ap *AdapterProcessLogger) ProcessFail()

func (*AdapterProcessLogger) ProcessStart

func (ap *AdapterProcessLogger) ProcessStart(name string)

func (*AdapterProcessLogger) ProcessStep

func (ap *AdapterProcessLogger) ProcessStep(name string)

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 sink for ShowInCompacted()-tagged records
	IsTTY      bool      // whether TTYWriter is a terminal (enables the terminal sink at all)
	// Interactive enables the pinned pterm progress bar. False (e.g. with -v) keeps the terminal
	// sink but renders plain linear lines with no pinned block.
	Interactive bool
	// Verbose (-v) shows every Info+ record on the terminal, not just the 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 and IsTTY is true, receives ShowInCompacted()-tagged records (the terminal).

type TerminalUIHandler

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

TerminalUIHandler is a dual-sink slog.Handler.

  • File sink: receives every enabled record (JSON).
  • TTY sink: receives only records tagged with ShowInCompacted(), and only when stdout is a terminal.

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