Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorized attribute #59

Open
tommsawyer opened this issue Mar 26, 2024 · 2 comments
Open

Colorized attribute #59

tommsawyer opened this issue Mar 26, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@tommsawyer
Copy link

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!

@lmittmann lmittmann self-assigned this Mar 29, 2024
@lmittmann lmittmann added the bug Something isn't working label Mar 30, 2024
@jose-tim
Copy link

jose-tim commented Apr 1, 2024

In case my idea is fine for the lib, I solved this issue for me with:

imagen

imagen

imagen

imagen

@mologie
Copy link

mologie commented May 9, 2024

Careful though, if this is solved with just a prefix then an attacker could exploit this to make your terminal do funky things by if they merely control some to-be-logged value (which they usually do).

If this becomes a library feature then imo it should be 1) behind an off-by-default flag (for e.g. libraries) and 2) a special type to wrap unsafe "raw" strings with, that should not be escaped when being printed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants