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

testing/slogtest: Parsing example fails when level is Error or Warn #62115

Closed
varun06 opened this issue Aug 17, 2023 · 4 comments
Closed

testing/slogtest: Parsing example fails when level is Error or Warn #62115

varun06 opened this issue Aug 17, 2023 · 4 comments
Assignees
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@varun06
Copy link

varun06 commented Aug 17, 2023

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

$ go version
go version go1.21.0 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/varun/Library/Caches/go-build'
GOENV='/Users/varun/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/varun/go/pkg/mod'
GONOPROXY='*.abc.com'
GONOSUMDB='*.abc.com'
GOOS='darwin'
GOPATH='/Users/varun/go'
GOPRIVATE='*.abc.com'
GOPROXY='direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/varun/test/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/60/qpt9s9_s7lq9g9w7p53079nr0000gn/T/go-build3087313581=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Copied example from package docs, added a level and tried running it.

func TestCustomHandler(t *testing.T) {
	var buf bytes.Buffer
	h := slog.NewJSONHandler(&buf, &slog.HandlerOptions{
		Level: slog.LevelError,
	})

	results := func() []map[string]any {
		var ms []map[string]any
		for _, line := range bytes.Split(buf.Bytes(), []byte{'\n'}) {
			if len(line) == 0 {
				continue
			}
			var m map[string]any
			if err := json.Unmarshal(line, &m); err != nil {
				t.Fatal(err)
			}
			ms = append(ms, m)
		}
		return ms
	}
	err := slogtest.TestHandler(h, results)
	if err != nil {
		t.Fatal(err)
	}
}

What did you expect to see?

I expected to see the test pass.

What did you see instead?

--- FAIL: TestCustomHandler (0.00s)
    /Users/varun/code/logger/multihandler_test.go:34: got 0 results, want 15

NOTE: Same test passes if Level is changed to Info or Debug.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 17, 2023
@dmitshur dmitshur added this to the Backlog milestone Aug 17, 2023
@dmitshur
Copy link
Contributor

CC @jba.

@jba jba added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 17, 2023
@jba jba self-assigned this Aug 17, 2023
@seankhliao
Copy link
Member

isn't this like #61706 (comment) where we don't expect it to pass?

@jba
Copy link
Contributor

jba commented Aug 17, 2023

This is WAI. The slogtest package runs all its tests at Info level, so the logger must enable Info.

But the documentation could be improved.

@gopherbot
Copy link

Change https://go.dev/cl/520676 mentions this issue: testing/slogtest: clarify that passed handler must be enabled at info

@dmitshur dmitshur modified the milestones: Backlog, Go1.22 Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants