fix(diary): skip HideOnTTY records only on a terminal console, also with forced color (IN-01, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:41:07 +01:00
parent f91d70d05e
commit 3520d5d480
8 changed files with 75 additions and 23 deletions

View File

@@ -40,7 +40,9 @@ type ConsoleOptions struct {
// the message and attribute values, so logged data cannot inject terminal
// escape sequences. Each line is written with one Write call, serialized
// across the handler and its WithAttrs and WithGroup clones; while a bonfire
// live view runs, lines print above it through bonfire.PrintAbove.
// live view runs, lines print above it through bonfire.PrintAbove. Records
// marked with HideOnTTY are skipped only when w is a terminal (checked once
// here), so forced color on a redirected stream keeps them.
func NewConsoleHandler(w io.Writer, opts ConsoleOptions) slog.Handler {
source := new(atomic.Bool)
source.Store(opts.AddSource)
@@ -53,11 +55,12 @@ func newConsoleHandler(w io.Writer, opts ConsoleOptions, source *atomic.Bool) *c
level = slog.LevelInfo
}
return &consoleHandler{
out: &colorprofile.Writer{Forward: w, Profile: opts.Profile},
mu: new(sync.Mutex),
level: level,
source: source,
palette: bonfire.DefaultPalette(),
out: &colorprofile.Writer{Forward: w, Profile: opts.Profile},
terminal: isTerminal(w),
mu: new(sync.Mutex),
level: level,
source: source,
palette: bonfire.DefaultPalette(),
}
}
@@ -67,11 +70,15 @@ type consoleAttr struct {
}
type consoleHandler struct {
out io.Writer
mu *sync.Mutex
level slog.Leveler
source *atomic.Bool
palette bonfire.Palette
out io.Writer
mu *sync.Mutex
level slog.Leveler
source *atomic.Bool
palette bonfire.Palette
// terminal is true when the handler's writer is a terminal, checked
// once when the handler is built: only then are HideOnTTY records
// skipped.
terminal bool
component string
attrs []consoleAttr
groups []string
@@ -114,9 +121,10 @@ func (h *consoleHandler) WithGroup(name string) slog.Handler {
}
func (h *consoleHandler) Handle(ctx context.Context, r slog.Record) error {
// The styled console is read by a person; facts already shown as a
// block (HideOnTTY) are not repeated as log lines.
if hiddenOnTTY(ctx) {
// A console on a terminal is read by a person; facts already shown as
// a block (HideOnTTY) are not repeated as log lines. Forced color on a
// redirected stream keeps them.
if h.terminal && hiddenOnTTY(ctx) {
return nil
}
component := h.component