charmbracelet / log

A minimal, colorful Go logging library 🪵
MIT License
2.4k stars 67 forks source link

Escaped escape codes when overriding ValueStyle and value needs quoting #19

Closed tombell closed 1 year ago

tombell commented 1 year ago

I was trying to override log.ValueStyle to add some colour, however due to it being quoted after the ValueStyle.Render(val) the added escape codes themselves end up escaped.

package main

import (
    "os"

    "github.com/charmbracelet/lipgloss"
    "github.com/charmbracelet/log"
)

func main() {
    logger := log.New(
        log.WithOutput(os.Stderr),
         log.WithTimestamp()
    )
    log.ValueStyle = lipgloss.NewStyle().
        Foreground(lipgloss.AdaptiveColor{Light: "208", Dark: "192"})
    logger.Info("testing overriding global styles", "key", "val", "foo", true)
}

This results in the following output in the terminal

charmbracelet/log output

The expected output would be

expected charmbracelet/log output

I would guess that the fix would be to call lipgloss.Render(val) after the value has been escaped to prevent the lipgloss escape codes from being escaped as part of the value.