Skip to content

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

@FIL033

Description

@FIL033

When using tint.Attr in the ReplaceAttr function to color a slog.LevelKey, the color "bleeds" and is applied not only to that attribute but also to slog.SourceKey.

Code to Reproduce

package main

import (
	"log/slog"
	"os"

	"github.com/lmittmann/tint"
)

func main() {
	handler := tint.NewHandler(os.Stdout, &tint.Options{
		Level:      slog.LevelDebug,
		AddSource:  true,
		ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
			if a.Key == slog.LevelKey {
				if lvl, ok := a.Value.Any().(slog.Level); ok && lvl == slog.LevelDebug {
					return tint.Attr(13, a) // Expected: only "DBG" should be colored
				}
			}
			return a
		},
	})
	
	logger := slog.New(handler)
	logger.Debug("test message", "key", "value")
}

Expected Behavior

Only the level text ("DBG") should be colored.

Actual Behavior

The color is applied to both the level and the source location (e.g., "DBG issue-folder/test.go:27").

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions