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

x/exp/slog: unexpected behavior when using NewJSONHandler #56087

Closed
AndrewHarrisSPU opened this issue Oct 6, 2022 · 2 comments
Closed

x/exp/slog: unexpected behavior when using NewJSONHandler #56087

AndrewHarrisSPU opened this issue Oct 6, 2022 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@AndrewHarrisSPU
Copy link

AndrewHarrisSPU commented Oct 6, 2022

What version of Go are you using (go version)?

go version go1.19rc2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOOS="darwin"

What did you do?

Using the slog package, ran the following program, as well as one with NewTextHandler

func main() {
	log := slog.New( slog.NewJSONHandler(os.Stdout)).With("job", "Eulerian Gophers" )
	log = log.With( "id", 1 )
	log.Log( slog.InfoLevel, "", "total", 5050 )
}

func main() {
	scully := slog.New( slog.NewTextHandler(os.Stdout))
	scully.Error( "👩‍🦰", errors.New("🛸"))
}

What did you expect to see?

For the first program, JSON parseable by gojq

For the second program, a log message showing some error associating Agent Scully and a UFO

(I don't have the expectation that slog is anything but /x/exp, but just noticed these things)

What did you see instead?

For the first program:

"level":"INFO","msg":"","job":"Eulerian Gophers""id":1,"total":5050}

this isn't parsed because there should be a [edit] a comma here: "Eulerian Gophers","id". There is a similar glitch with TextHandler output, and I guess it's the preformatting. With("job", "Eulerian Gophers", "id", 1 ) eliminates the glitch.

For the second program, using

level=ERROR msg="👩\u200d🦰" err=🛸

Using JSON seems to avoid escaping the zero width joiner.

[edit: fixed a few details in description that were backwards]

@dmitshur dmitshur changed the title affected/package: /x/exp/slog x/exp/slog: unexpected behavior when using NewJSONHandler Oct 6, 2022
@gopherbot gopherbot added this to the Unreleased milestone Oct 6, 2022
@seankhliao
Copy link
Member

cc @jba

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 7, 2022
@jba
Copy link
Contributor

jba commented Oct 15, 2022

The first problem is fixed at master.

The second is WAI. TextHandler uses strconv.Quote to quote strings with non-printing characters, like the zero-width joiner. You can get back the original with strconv.Unquote. JSONHandler uses the same escaping as json.Marshal, which does not escape non-printing characters. We wanted to make parsing of the text format straightforward; we don't want it to depend on anything in the encoding/json package.

@jba jba closed this as completed Oct 15, 2022
@golang golang locked and limited conversation to collaborators Oct 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants