Skip to content

fix: reset color after tint.Attr for all levels#101

Merged
lmittmann merged 4 commits intolmittmann:mainfrom
FIL033:main
Feb 2, 2026
Merged

fix: reset color after tint.Attr for all levels#101
lmittmann merged 4 commits intolmittmann:mainfrom
FIL033:main

Conversation

@FIL033
Copy link
Copy Markdown
Contributor

@FIL033 FIL033 commented Jan 12, 2026

Description

Fixes a bug where tint.Attr used in ReplaceAttr doesn't reset ANSI colors for log levels below slog.LevelInfo (e.g., DEBUG), causing the color to "bleed" into subsequent log parts like source location.

Problem

When using tint.Attr in ReplaceAttr to colorize the slog.LevelKey attribute, the color was not being properly reset for levels < slog.LevelInfo. This resulted in the source location and message also being colored instead of just the level text.

Solution

Modified the condition in appendTintLevel to reset color when color >= 0 (i.e., when a custom color is applied via tint.Attr), regardless of the log level. Previously, color reset only happened for level >= slog.LevelInfo.

Change:

// Before
if !h.opts.NoColor && level >= slog.LevelInfo {
    buf.WriteString(ansiReset)
}

// After
if !h.opts.NoColor && (color >= 0 || level >= slog.LevelInfo) {
    buf.WriteString(ansiReset)
}

Related Issue

Fixes the issue described in #100

@lmittmann
Copy link
Copy Markdown
Owner

Thanks for raising this bug. Could you append a test-case to handlerTests to reproduce the issue?

@FIL033
Copy link
Copy Markdown
Contributor Author

FIL033 commented Jan 16, 2026

Thanks for raising this bug. Could you append a test-case to handlerTests to reproduce the issue?

Done

@lmittmann lmittmann changed the title fix: reset color after tint.Attr for all levels fix: reset color after tint.Attr for all levels Feb 2, 2026
@lmittmann
Copy link
Copy Markdown
Owner

thank's for the fix!

@lmittmann lmittmann merged commit 3a1d4c3 into lmittmann:main Feb 2, 2026
2 checks passed
Copy link
Copy Markdown
Contributor Author

@FIL033 FIL033 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmittmann After changes, this test no longer verifies the fix for the linked issue #100 .
The original bug was related to incorrect handling of log levels below slog.LevelInfo (e.g., slog.LevelDebug). The current version of the test uses the Info level, which was not affected by the bug, so it no longer serves as a regression test for issue #100 .

@lmittmann
Copy link
Copy Markdown
Owner

🤦🏻‍♂️ fixed in ccd13bc. thanks for noticing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tint.Attr in ReplaceAttr colors subsequent log parts, not just the target attribute

2 participants