-
Notifications
You must be signed in to change notification settings - Fork 63
Colorized attribute #59
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Hi! I'm trying to add syntax highlighting for my SQL queries in logs.
There is a library that does it pretty well:
package main
import (
"bytes"
"os"
"github.com/alecthomas/chroma/quick"
)
func main() {
var q bytes.Buffer
quick.Highlight(&q, "SELECT 1 + 1", "sql", "terminal16m", "monokai")
os.Stderr.Write(q.Bytes()) // output will be colorized, as expected
}But when I try to add colorized SQL as slog.Attr, it will print escaped sequence instead:
package main
import (
"bytes"
"log/slog"
"os"
"time"
"github.com/alecthomas/chroma/quick"
"github.com/lmittmann/tint"
)
func main() {
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.TimeOnly,
}),
))
var q bytes.Buffer
quick.Highlight(&q, "SELECT 1 + 1", "sql", "terminal16m", "monokai")
// prints something like
// 18:50:29 INF SQL query query="\x1b[38;2;102;217;239mSELECT\x1b[0m\x1b[38;2;248;248;242m \x1b[0m\x1b[38;2;174;129;255m1\x1b[0m\x1b[38;2;24
slog.Info("SQL query", "query", q.String())
}I believe this is happening because of strconv.AppendQuote, which you are using to quote strings here. AppendQuote escapes all the colors sequences.
Seems like you're passing "quote=true" everywhere and I cannot disable this behavior using ReplaceAttrs or something like that.
Am I missing something? Is there a way to preserve colors for some attributes? Thanks!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working